home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / ange-ftp.el.z / ange-ftp.el
Encoding:
Text File  |  1998-10-28  |  204.0 KB  |  5,468 lines

  1. ;;; ange-ftp.el --- transparent FTP support for GNU Emacs
  2.  
  3. ;; Copyright (C) 1989,90,91,92,93,94,95,96  Free Software Foundation, Inc.
  4.  
  5. ;; Author: Andy Norman (ange@hplb.hpl.hp.com)
  6. ;; Maintainer: FSF
  7. ;; Keywords: comm
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This package attempts to make accessing files and directories using FTP
  29. ;; from within GNU Emacs as simple and transparent as possible.  A subset of
  30. ;; the common file-handling routines are extended to interact with FTP.
  31.  
  32. ;; Usage:
  33. ;;
  34. ;; Some of the common GNU Emacs file-handling operations have been made
  35. ;; FTP-smart.  If one of these routines is given a filename that matches
  36. ;; '/user@host:name' then it will spawn an FTP process connecting to machine
  37. ;; 'host' as account 'user' and perform its operation on the file 'name'.
  38. ;;
  39. ;; For example: if find-file is given a filename of:
  40. ;;
  41. ;;   /ange@anorman:/tmp/notes
  42. ;;
  43. ;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as
  44. ;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
  45. ;; contents of that file as if it were on the local filesystem.  If ange-ftp
  46. ;; needs a password to connect then it reads one in the echo area.
  47.  
  48. ;; Extended filename syntax:
  49. ;;
  50. ;; The default extended filename syntax is '/user@host:name', where the
  51. ;; 'user@' part may be omitted.  This syntax can be customised to a certain
  52. ;; extent by changing ange-ftp-name-format.  There are limitations.
  53. ;;
  54. ;; If the user part is omitted then ange-ftp generates a default user
  55. ;; instead whose value depends on the variable ange-ftp-default-user.
  56.  
  57. ;; Passwords:
  58. ;;
  59. ;; A password is required for each host/user pair.  Ange-ftp reads passwords
  60. ;; as needed.  You can also specify a password with ange-ftp-set-passwd, or
  61. ;; in a *valid* ~/.netrc file.
  62.  
  63. ;; Passwords for user "anonymous":
  64. ;;
  65. ;; Passwords for the user "anonymous" (or "ftp") are handled
  66. ;; specially.  The variable `ange-ftp-generate-anonymous-password'
  67. ;; controls what happens: if the value of this variable is a string,
  68. ;; then this is used as the password; if non-nil (the default), then
  69. ;; the value of `user-mail-address' is used; if nil then the user
  70. ;; is prompted for a password as normal.
  71.  
  72. ;; "Dumb" UNIX hosts:
  73. ;;
  74. ;; The FTP servers on some UNIX machines have problems if the 'ls' command is
  75. ;; used.
  76. ;;
  77. ;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
  78. ;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
  79. ;; that this change will take effect for the current GNU Emacs session only.
  80. ;; See below for a discussion of non-UNIX hosts.  If a large number of
  81. ;; machines with similar hostnames have this problem then it is easier to set
  82. ;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp
  83. ;; is unable to automatically recognize dumb unix hosts.
  84.  
  85. ;; File name completion:
  86. ;;
  87. ;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
  88. ;; To do filename completion, ange-ftp needs a listing from the remote host.
  89. ;; Therefore, for very slow connections, it might not save any time.
  90.  
  91. ;; FTP processes:
  92. ;;
  93. ;; When ange-ftp starts up an FTP process, it leaves it running for speed
  94. ;; purposes.  Some FTP servers will close the connection after a period of
  95. ;; time, but ange-ftp should be able to quietly reconnect the next time that
  96. ;; the process is needed.
  97. ;;
  98. ;; Killing the "*ftp user@host*" buffer also kills the ftp process.
  99. ;; This should not cause ange-ftp any grief.
  100.  
  101. ;; Binary file transfers:
  102. ;;
  103. ;; By default ange-ftp transfers files in ASCII mode.  If a file being
  104. ;; transferred matches the value of ange-ftp-binary-file-name-regexp then
  105. ;; binary mode is used for that transfer.
  106.  
  107. ;; Account passwords:
  108. ;;
  109. ;; Some FTP servers require an additional password which is sent by the
  110. ;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
  111. ;; specify an account password by either calling ange-ftp-set-account, or by
  112. ;; specifying an account token in the .netrc file.  If the account password
  113. ;; is set by either of these methods then ange-ftp will issue an ACCOUNT
  114. ;; command upon starting the FTP process.
  115.  
  116. ;; Preloading:
  117. ;;
  118. ;; ange-ftp can be preloaded, but must be put in the site-init.el file and
  119. ;; not the site-load.el file in order for the documentation strings for the
  120. ;; functions being overloaded to be available.
  121.  
  122. ;; Status reports:
  123. ;;
  124. ;; Most ange-ftp commands that talk to the FTP process output a status
  125. ;; message on what they are doing.  In addition, ange-ftp can take advantage
  126. ;; of the FTP client's HASH command to display the status of transferring
  127. ;; files and listing directories.  See the documentation for the variables
  128. ;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
  129. ;; ange-ftp-process-verbose for more details.
  130.  
  131. ;; Gateways:
  132. ;;
  133. ;; Sometimes it is necessary for the FTP process to be run on a different
  134. ;; machine than the machine running GNU Emacs.  This can happen when the
  135. ;; local machine has restrictions on what hosts it can access.
  136. ;;
  137. ;; ange-ftp has support for running the ftp process on a different (gateway)
  138. ;; machine.  The way it works is as follows:
  139. ;;
  140. ;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
  141. ;;    that doesn't have the access restrictions.
  142. ;;
  143. ;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
  144. ;;    that matches hosts that can be contacted from running a local ftp
  145. ;;    process, but fails to match hosts that can't be accessed locally.  For
  146. ;;    example:
  147. ;;
  148. ;;    "\\.hp\\.com$\\|^[^.]*$"
  149. ;;
  150. ;;    will match all hosts that are in the .hp.com domain, or don't have an
  151. ;;    explicit domain in their name, but will fail to match hosts with
  152. ;;    explicit domains or that are specified by their ip address.
  153. ;;
  154. ;; 3) Using NFS and symlinks, make sure that there is a shared directory with
  155. ;;    the *same* name between the local machine and the gateway machine.
  156. ;;    This directory is necessary for temporary files created by ange-ftp.
  157. ;;
  158. ;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
  159. ;;    this directory plus an identifying filename prefix.  For example:
  160. ;;
  161. ;;    "/nfs/hplose/ange/ange-ftp"
  162. ;;
  163. ;;    where /nfs/hplose/ange is a directory that is shared between the
  164. ;;    gateway machine and the local machine.
  165. ;;
  166. ;; The simplest way of getting a ftp process running on the gateway machine
  167. ;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
  168. ;; can't do this for some reason such as security then points 7 onwards will
  169. ;; discuss an alternative approach.
  170. ;;
  171. ;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
  172. ;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
  173. ;;
  174. ;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
  175. ;;    isn't already.  This tells ange-ftp that you are using a remote shell
  176. ;;    rather than logging in using telnet or rlogin.
  177. ;;
  178. ;; That should be all you need to allow ange-ftp to spawn a ftp process on
  179. ;; the gateway machine.  If you have to use telnet or rlogin to get to the
  180. ;; gateway machine then follow the instructions below.
  181. ;;
  182. ;; 7) Set the variable ange-ftp-gateway-program to the name of the program
  183. ;;    that lets you log onto the gateway machine.  This may be something like
  184. ;;    telnet or rlogin.
  185. ;;
  186. ;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
  187. ;;    expression that matches the prompt you get when you login to the
  188. ;;    gateway machine.  Be very specific here; this regexp must not match
  189. ;;    *anything* in your login banner except this prompt.
  190. ;;    shell-prompt-pattern is far too general as it appears to match some
  191. ;;    login banners from Sun machines.  For example:
  192. ;;
  193. ;;    "^$*$ *"
  194. ;;
  195. ;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
  196. ;;    ange-ftp know that it has to "hand-hold" the login to the gateway
  197. ;;    machine.
  198. ;;
  199. ;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
  200. ;;     that will put the pty connected to the gateway machine into a
  201. ;;     no-echoing mode, and will strip off carriage-returns from output from
  202. ;;     the gateway machine.  For example:
  203. ;;
  204. ;;     "stty -onlcr -echo"
  205. ;;
  206. ;;     will work on HP-UX machines, whereas:
  207. ;;
  208. ;;     "stty -echo nl"
  209. ;;
  210. ;;     appears to work for some Sun machines.
  211. ;;
  212. ;; That's all there is to it.
  213.  
  214. ;; Smart gateways:
  215. ;;
  216. ;; If you have a "smart" ftp program that allows you to issue commands like
  217. ;; "USER foo@bar" which do nice proxy things, then look at the variables
  218. ;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
  219. ;;
  220. ;; Otherwise, if there is an alternate ftp program that implements proxy in
  221. ;; a transparent way (i.e. w/o specifying the proxy host), that will
  222. ;; connect you directly to the desired destination host:
  223. ;; Set ange-ftp-gateway-ftp-program-name to that program's name.
  224. ;; Set ange-ftp-local-host-regexp to a value as stated earlier on.
  225. ;; Leave ange-ftp-gateway-host set to nil.
  226. ;; Set ange-ftp-smart-gateway to t.
  227.  
  228. ;; Tips for using ange-ftp:
  229. ;;
  230. ;; 1. For dired to work on a host which marks symlinks with a trailing @ in
  231. ;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
  232. ;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
  233. ;;    there is a chance you might connect to an ULTRIX machine (such as
  234. ;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
  235. ;;    the side effect that dired will have problems with symlinks whose names
  236. ;;    end in an @.  If you get yourself into this situation then editing
  237. ;;    dired's ls-switches to remove "F", will temporarily fix things.
  238. ;;
  239. ;; 2. If you know that you are connecting to a certain non-UNIX machine
  240. ;;    frequently, and ange-ftp seems to be unable to guess its host-type,
  241. ;;    then setting the appropriate host-type regexp
  242. ;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
  243. ;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
  244. ;;    ange-ftp's inability to recognize the host-type as a bug.
  245. ;;
  246. ;; 3. For slow connections, you might get "listing unreadable" error
  247. ;;    messages, or get an empty buffer for a file that you know has something
  248. ;;    in it. The solution is to increase the value of ange-ftp-retry-time.
  249. ;;    Its default value is 5 which is plenty for reasonable connections.
  250. ;;    However, for some transatlantic connections I set this to 20.
  251. ;;
  252. ;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
  253. ;;    copying the file to the local machine, compressing it there, and then
  254. ;;    sending it back. Binary file transfers between machines of different
  255. ;;    architectures can be a risky business. Test things out first on some
  256. ;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
  257. ;;    moving them through the local machine. Again, be careful when doing
  258. ;;    this with binary files on non-Unix machines.
  259. ;;
  260. ;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
  261. ;;    (list of dired commands for which confirmation is not asked).  You
  262. ;;    might want to reconsider your setting of this variable, because you
  263. ;;    might want confirmation for more commands on remote direds than on
  264. ;;    local direds. For example, I strongly recommend that you not include
  265. ;;    compress and uncompress in this list. If there is enough demand it
  266. ;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
  267. ;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
  268. ;;    is a list of commands for which confirmation would be suppressed.  Then
  269. ;;    remote dired listings would take their (buffer-local) value of
  270. ;;    dired-no-confirm from this alist. Who votes for this?
  271.  
  272. ;; ---------------------------------------------------------------------
  273. ;; Non-UNIX support:
  274. ;; ---------------------------------------------------------------------
  275.  
  276. ;; VMS support:
  277. ;;
  278. ;; Ange-ftp has full support for VMS hosts.  It
  279. ;; should be able to automatically recognize any VMS machine. However, if it
  280. ;; fails to do this, you can use the command ange-ftp-add-vms-host.  As well,
  281. ;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We
  282. ;; would be grateful if you would report any failures to automatically
  283. ;; recognize a VMS host as a bug.
  284. ;;
  285. ;; Filename Syntax:
  286. ;;
  287. ;; For ease of *implementation*, the user enters the VMS filename syntax in a
  288. ;; UNIX-y way.  For example:
  289. ;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
  290. ;; would be entered as:
  291. ;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
  292. ;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
  293. ;;  [.CSV.POLICY]RULES.MEM
  294. ;; you would type:
  295. ;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
  296. ;;
  297. ;; A legal VMS filename is of the form: FILE.TYPE;##
  298. ;; where FILE can be up to 39 characters
  299. ;;       TYPE can be up to 39 characters
  300. ;;       ## is a version number (an integer between 1 and 32,767)
  301. ;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
  302. ;; $ cannot begin a filename, and - cannot be used as the first or last
  303. ;; character.
  304. ;;
  305. ;; Tips:
  306. ;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
  307. ;;    Therefore, to access a VMS file, you must enter the filename with upper
  308. ;;    case letters.
  309. ;; 2. To access the latest version of file under VMS, you use the filename
  310. ;;    without the ";" and version number. You should always edit the latest
  311. ;;    version of a file. If you want to edit an earlier version, copy it to a
  312. ;;    new file first. This has nothing to do with ange-ftp, but is simply
  313. ;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
  314. ;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
  315. ;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
  316. ;;    that VMS will not allow you to save the file because it will refuse to
  317. ;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
  318. ;;    attach the buffer to this file. To get out of this situation, M-x
  319. ;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
  320. ;;    latest version of the file. For this reason, in dired "f"
  321. ;;    (dired-find-file), always loads the file sans version, whereas "v",
  322. ;;    (dired-view-file), always loads the explicit version number. The
  323. ;;    reasoning being that it reasonable to view old versions of a file, but
  324. ;;    not to edit them.
  325. ;; 3. EMACS has a feature in which it does environment variable substitution
  326. ;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
  327. ;;    by typing $$.
  328.  
  329. ;; MTS support:
  330. ;;
  331. ;; Ange-ftp has full support for hosts running
  332. ;; the Michigan terminal system.  It should be able to automatically
  333. ;; recognize any MTS machine. However, if it fails to do this, you can use
  334. ;; the command ange-ftp-add-mts-host.  As well, you can set the variable
  335. ;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you
  336. ;; would report any failures to automatically recognize a MTS host as a bug.
  337. ;;
  338. ;; Filename syntax:
  339. ;; 
  340. ;; MTS filenames are entered in a UNIX-y way. For example, if your account
  341. ;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
  342. ;; entered as
  343. ;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
  344. ;; In other words, MTS accounts are treated as UNIX directories. Of course,
  345. ;; to access a file in another account, you must have access permission for
  346. ;; it.  If FILE were in your own account, then you could enter it in a
  347. ;; relative name fashion as
  348. ;;   /YYYY@mtsg.ubc.ca:FILE
  349. ;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
  350. ;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
  351. ;; like.) MTS filenames are always in upper case, and hence be sure to enter
  352. ;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
  353. ;; is.
  354.  
  355. ;; CMS support:
  356. ;; 
  357. ;; Ange-ftp has full support for hosts running
  358. ;; CMS.  It should be able to automatically recognize any CMS machine.
  359. ;; However, if it fails to do this, you can use the command
  360. ;; ange-ftp-add-cms-host.  As well, you can set the variable
  361. ;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you
  362. ;; would report any failures to automatically recognize a CMS host as a bug.
  363. ;; 
  364. ;; Filename syntax:
  365. ;;
  366. ;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
  367. ;; treated as UNIX directories. For example to access the file READ.ME in
  368. ;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
  369. ;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
  370. ;; If *.301 is the default minidisk for this account, you could access
  371. ;; FOO.BAR on this minidisk as
  372. ;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
  373. ;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
  374. ;; up to 8 characters. Again, beware that CMS filenames are always upper
  375. ;; case, and hence must be entered as such.
  376. ;;
  377. ;; Tips:
  378. ;; 1. CMS machines, with the exception of anonymous accounts, nearly always
  379. ;;    need an account password. To have ange-ftp send an account password,
  380. ;;    you can either include it in your .netrc file, or use
  381. ;;    ange-ftp-set-account.
  382. ;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
  383. ;;    can fix this.
  384. ;;
  385. ;; ------------------------------------------------------------------
  386. ;; Bugs:
  387. ;; ------------------------------------------------------------------
  388. ;; 
  389. ;; 1. Umask problems:
  390. ;;    Be warned that files created by using ange-ftp will take account of the
  391. ;;    umask of the ftp daemon process rather than the umask of the creating
  392. ;;    user.  This is particularly important when logging in as the root user.
  393. ;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
  394. ;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
  395. ;;    suspect that there is something similar on other systems.
  396. ;;
  397. ;; 2. Some combinations of FTP clients and servers break and get out of sync
  398. ;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
  399. ;;    machines cause this problem for some FTP clients. Using
  400. ;;    ange-ftp-kill-ftp-process can restart the ftp process, which
  401. ;;    should get things back in sync.
  402. ;;
  403. ;; 3. Ange-ftp does not check to make sure that when creating a new file,
  404. ;;    you provide a valid filename for the remote operating system.
  405. ;;    If you do not, then the remote FTP server will most likely
  406. ;;    translate your filename in some way. This may cause ange-ftp to
  407. ;;    get confused about what exactly is the name of the file. The
  408. ;;    most common causes of this are using lower case filenames on systems
  409. ;;    which support only upper case, and using filenames which are too
  410. ;;    long.
  411. ;;
  412. ;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
  413. ;;
  414. ;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
  415. ;;    for some reason creates a FTP process that only talks via pipes then
  416. ;;    ange-ftp won't be getting the information it requires at the time that
  417. ;;    it wants it since pipes flush at different times to pty's.  One
  418. ;;    disgusting way around this problem is to talk to the FTP process via
  419. ;;    rlogin which does the 'right' things with pty's.
  420. ;;
  421. ;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
  422. ;;    worried about this too much. Eventually, we should have some caching
  423. ;;    of the current minidisk.
  424. ;;    
  425. ;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
  426. ;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
  427. ;;    to it. This is (understandably) beyond ange-ftp.
  428. ;;
  429. ;; 8. Remote to remote copying of files on non-Unix machines can be risky.
  430. ;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
  431. ;;    will use binary mode for the copy. Between systems of different
  432. ;;    architecture, this still may not be enough to guarantee the integrity
  433. ;;    of binary files. Binary file transfers from VMS machines are
  434. ;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
  435. ;;    an alist of OS type, regexp pairs?
  436. ;;
  437. ;; 9. The code to do compression of files over ftp is not as careful as it
  438. ;;    should be. It deletes the old remote version of the file, before
  439. ;;    actually checking if the local to remote transfer of the compressed
  440. ;;    file succeeds. Of course to delete the original version of the file
  441. ;;    after transferring the compressed version back is also dangerous,
  442. ;;    because some OS's have severe restrictions on the length of filenames,
  443. ;;    and when the compressed version is copied back the "-Z" or ".Z" may be
  444. ;;    truncated. Then, ange-ftp would delete the only remaining version of
  445. ;;    the file.  Maybe ange-ftp should make backups when it compresses files
  446. ;;    (of course, the backup "~" could also be truncated off, sigh...).
  447. ;;    Suggestions?
  448. ;;
  449. ;; 10. If a dir listing is attempted for an empty directory on (at least
  450. ;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
  451. ;;     I don't know how to get ange-ftp work to around it.
  452. ;;
  453. ;; 11. Bombs on filenames that start with a space. Deals well with filenames
  454. ;;     containing spaces, but beware that the remote ftpd may not like them
  455. ;;     much.
  456. ;;
  457. ;; 12. The dired support for non-Unix-like systems does not currently work.
  458. ;;     It needs to be reimplemented by modifying the parse-...-listing
  459. ;;    functions to convert the directory listing to ls -l format.
  460. ;; 
  461. ;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
  462. ;;     with a trailing @ in a ls -alF listing. In order to account for this
  463. ;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
  464. ;;     parsing a listing with the F switch. This will cause ange-ftp to
  465. ;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
  466. ;;     ends in an @. ange-ftp will correct itself if you take F out of the
  467. ;;     dired ls switches (C-u s will allow you to edit the switches). The
  468. ;;     dired buffer will be automatically reverted, which will allow ange-ftp
  469. ;;     to fix its files hashtable.  A cookie to anyone who can think of a
  470. ;;     fast, sure-fire way to recognize ULTRIX over ftp.
  471.  
  472. ;; If you find any bugs or problems with this package, PLEASE either e-mail
  473. ;; the above author, or send a message to the ange-ftp-lovers mailing list
  474. ;; below.  Ideas and constructive comments are especially welcome.
  475.  
  476. ;; ange-ftp-lovers:
  477. ;;
  478. ;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
  479. ;; users of ange-ftp are welcome to subscribe (see below) and to discuss
  480. ;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
  481. ;; the mailing list.
  482. ;;
  483. ;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
  484. ;; list, please mail one of the following addresses:
  485. ;;
  486. ;;     ange-ftp-lovers-request@anorman.hpl.hp.com
  487. ;; or
  488. ;;     ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com
  489. ;;
  490. ;; Please don't forget the -request part.
  491. ;;
  492. ;; For mail to be posted directly to ange-ftp-lovers, send to one of the
  493. ;; following addresses:
  494. ;; 
  495. ;;     ange-ftp-lovers@anorman.hpl.hp.com
  496. ;; or
  497. ;;     ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com
  498. ;;
  499. ;; Alternatively, there is a mailing list that only gets announcements of new
  500. ;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
  501. ;; subscribed to by e-mailing to the -request address as above.  Please make
  502. ;; it clear in the request which mailing list you wish to join.
  503.  
  504. ;; The latest version of ange-ftp can usually be obtained via anonymous ftp
  505. ;; from:
  506. ;;     alpha.gnu.ai.mit.edu:ange-ftp/ange-ftp.tar.Z
  507. ;; or:
  508. ;;     ugle.unit.no:/pub/gnu/emacs-lisp/ange-ftp.tar.Z
  509. ;; or:
  510. ;;   archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages/ange-ftp.tar.Z
  511.  
  512. ;; The archives for ange-ftp-lovers can be found via anonymous ftp under:
  513. ;;
  514. ;;     ftp.reed.edu:pub/mailing-lists/ange-ftp/
  515.  
  516. ;; -----------------------------------------------------------
  517. ;; Technical information on this package:
  518. ;; -----------------------------------------------------------
  519.  
  520. ;; ange-ftp works by putting a handler on file-name-handler-alist
  521. ;; which is called by many primitives, and a few non-primitives,
  522. ;; whenever they see a file name of the appropriate sort.
  523.  
  524. ;; Checklist for adding non-UNIX support for TYPE
  525. ;; 
  526. ;; The following functions may need TYPE versions:
  527. ;; (not all functions will be needed for every OS)
  528. ;;
  529. ;; ange-ftp-fix-name-for-TYPE
  530. ;; ange-ftp-fix-dir-name-for-TYPE
  531. ;; ange-ftp-TYPE-host
  532. ;; ange-ftp-TYPE-add-host
  533. ;; ange-ftp-parse-TYPE-listing
  534. ;; ange-ftp-TYPE-delete-file-entry
  535. ;; ange-ftp-TYPE-add-file-entry
  536. ;; ange-ftp-TYPE-file-name-as-directory
  537. ;; ange-ftp-TYPE-make-compressed-filename
  538. ;; ange-ftp-TYPE-file-name-sans-versions
  539. ;;
  540. ;; Variables:
  541. ;;
  542. ;; ange-ftp-TYPE-host-regexp
  543. ;; May need to add TYPE to ange-ftp-dumb-host-types
  544. ;;
  545. ;; Check the following functions for OS dependent coding:
  546. ;;
  547. ;; ange-ftp-host-type
  548. ;; ange-ftp-guess-host-type
  549. ;; ange-ftp-allow-child-lookup
  550.  
  551. ;; Host type conventions:
  552. ;;
  553. ;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
  554. ;; (mostly) follow the following conventions for remote host types.  At
  555. ;; least, I think that future code should try to follow these conventions,
  556. ;; and the current code should eventually be made compliant.
  557. ;;
  558. ;; nil = local host type, whatever that is (probably unix).
  559. ;;       Think nil as in "not a remote host". This value is used by
  560. ;;       ange-ftp-dired-host-type for local buffers.
  561. ;;
  562. ;; t = a remote host of unknown type. Think t is in true, it's remote.
  563. ;;     Currently, 'unix is used as the default remote host type.
  564. ;;     Maybe we should use t.
  565. ;;
  566. ;; 'type = a remote host of TYPE type.
  567. ;;
  568. ;; 'type:list = a remote host of TYPE type, using a specialized ftp listing
  569. ;;              program called list. This is currently only used for Unix
  570. ;;              dl (descriptive listings), when ange-ftp-dired-host-type
  571. ;;              is set to 'unix:dl.
  572.  
  573. ;; Bug report codes:
  574. ;;
  575. ;; Because of their naive faith in this code, there are certain situations
  576. ;; which the writers of this program believe could never happen. However,
  577. ;; being realists they have put calls to `error' in the program at these
  578. ;; points. These errors provide a code, which is an integer, greater than 1.
  579. ;; To aid debugging.  the error codes, and the functions in which they reside
  580. ;; are listed below.
  581. ;; 
  582. ;; 1: See ange-ftp-ls
  583. ;;
  584.  
  585. ;; -----------------------------------------------------------
  586. ;; Hall of fame:
  587. ;; -----------------------------------------------------------
  588. ;; 
  589. ;; Thanks to Roland McGrath for improving the filename syntax handling,
  590. ;; for suggesting many enhancements and for numerous cleanups to the code.
  591. ;;
  592. ;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
  593. ;;
  594. ;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
  595. ;; dired / shell auto-loading.
  596. ;;
  597. ;; Thanks to Sebastian Kremer for dired support and for many ideas and
  598. ;; bugfixes.
  599. ;;
  600. ;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
  601. ;; VOS support, and hostname completion.
  602. ;;
  603. ;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
  604. ;; with file-name expansion, efficiency worries, stylistic concerns and many
  605. ;; bugfixes.
  606. ;;
  607. ;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
  608. ;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
  609. ;; auto-recognition of the host type.
  610. ;;
  611. ;; Thanks to Dave Smith who wrote the info file for ange-ftp.
  612. ;;
  613. ;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
  614. ;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
  615. ;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
  616. ;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
  617. ;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
  618. ;; whose names I've forgotten who have helped to debug and fix problems with
  619. ;; ange-ftp.el.
  620.  
  621. ;;; Code:
  622.  
  623. (require 'comint)
  624.  
  625. ;;;; ------------------------------------------------------------
  626. ;;;; User customization variables.
  627. ;;;; ------------------------------------------------------------
  628.  
  629. (defvar ange-ftp-name-format
  630.   '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4))
  631.   "*Format of a fully expanded remote file name.
  632. This is a list of the form \(REGEXP HOST USER NAME\),
  633. where REGEXP is a regular expression matching
  634. the full remote name, and HOST, USER, and NAME are the numbers of
  635. parenthesized expressions in REGEXP for the components (in that order).")
  636.  
  637. ;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
  638. ;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
  639. ;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.
  640.  
  641. (defvar ange-ftp-multi-msgs
  642.   "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-"
  643.   "*Regular expression matching the start of a multiline ftp reply.")
  644.  
  645. (defvar ange-ftp-good-msgs
  646.   "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark"
  647.   "*Regular expression matching ftp \"success\" messages.")
  648.  
  649. ;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
  650. ;; Also CMS machines use a multiline 550- reply to say that you
  651. ;; don't have write permission. ange-ftp gets into multi-line skip
  652. ;; mode and hangs. Have it ignore 550- instead. It will then barf
  653. ;; when it gets the 550 line, as it should.
  654.  
  655. (defvar ange-ftp-skip-msgs
  656.   (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
  657.       "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
  658.       "^Data connection \\|"
  659.       "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye")
  660.   "*Regular expression matching ftp messages that can be ignored.")
  661.  
  662. (defvar ange-ftp-fatal-msgs
  663.   (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
  664.       "^No control connection\\|unknown host\\|^lost connection")
  665.   "*Regular expression matching ftp messages that indicate serious errors.
  666. These mean that the FTP process should (or already has) been killed.")
  667.  
  668. (defvar ange-ftp-gateway-fatal-msgs
  669.   "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
  670.   "*Regular expression matching login failure messages from rlogin/telnet.")
  671.  
  672. (defvar ange-ftp-xfer-size-msgs
  673.   "^150 .* connection for .* (\\([0-9]+\\) bytes)"
  674.   "*Regular expression used to determine the number of bytes in a FTP transfer.")
  675.  
  676. (defvar ange-ftp-tmp-name-template "/tmp/ange-ftp"
  677.   "*Template used to create temporary files.")
  678.  
  679. (defvar ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
  680.   "*Template used to create temporary files when ftp-ing through a gateway.
  681. Files starting with this prefix need to be accessible from BOTH the local
  682. machine and the gateway machine, and need to have the SAME name on both
  683. machines, that is, /tmp is probably NOT what you want, since that is rarely
  684. cross-mounted.")
  685.  
  686. (defvar ange-ftp-netrc-filename "~/.netrc"
  687.   "*File in .netrc format to search for passwords.")
  688.  
  689. (defvar ange-ftp-disable-netrc-security-check nil
  690.   "*If non-nil avoid checking permissions on the .netrc file.")
  691.  
  692. (defvar ange-ftp-default-user nil
  693.   "*User name to use when none is specified in a file name.
  694. If non-nil but not a string, you are prompted for the name.
  695. If nil, the value of `ange-ftp-netrc-default-user' is used.
  696. If that is nil too, then your login name is used.
  697.  
  698. Once a connection to a given host has been initiated, the user name
  699. and password information for that host are cached and re-used by
  700. ange-ftp.  Use `ange-ftp-set-user' to change the cached values,
  701. since setting `ange-ftp-default-user' directly does not affect
  702. the cached information.")  
  703.  
  704. (defvar ange-ftp-netrc-default-user nil
  705.   "Alternate default user name to use when none is specified.
  706. This variable is set from the `default' command in your `.netrc' file,
  707. if there is one.")
  708.  
  709. (defvar ange-ftp-default-password nil
  710.   "*Password to use when the user name equals `ange-ftp-default-user'.")
  711.  
  712. (defvar ange-ftp-default-account nil
  713.   "*Account to use when the user name equals `ange-ftp-default-user'.")
  714.  
  715. (defvar ange-ftp-netrc-default-password nil
  716.   "*Password to use when the user name equals `ange-ftp-netrc-default-user'.")
  717.  
  718. (defvar ange-ftp-netrc-default-account nil
  719.   "*Account to use when the user name equals `ange-ftp-netrc-default-user'.")
  720.  
  721. (defvar ange-ftp-generate-anonymous-password t
  722.   "*If t, use value of `user-mail-address' as password for anonymous ftp.
  723. If a string, then use that string as the password.
  724. If nil, prompt the user for a password.")
  725.  
  726. (defvar ange-ftp-dumb-unix-host-regexp nil
  727.   "*If non-nil, regexp matching hosts on which `dir' command lists directory.")
  728.  
  729. (defvar ange-ftp-binary-file-name-regexp
  730.   (concat "\\.[zZ]$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
  731.       "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
  732.       "\\.EXE\\(;[0-9]+\\)?$\\|\\.[zZ]-part-..$\\|\\.gz$\\|"
  733.       "\\.taz$\\|\\.tgz$")
  734.   "*If a file matches this regexp then it is transferred in binary mode.")
  735.  
  736. (defvar ange-ftp-gateway-host nil
  737.   "*Name of host to use as gateway machine when local FTP isn't possible.")
  738.  
  739. (defvar ange-ftp-local-host-regexp ".*"
  740.   "*Regexp selecting hosts which can be reached directly with ftp.
  741. For other hosts the FTP process is started on \`ange-ftp-gateway-host\'
  742. instead, and/or reached via \`ange-ftp-gateway-ftp-program-name\'.")
  743.  
  744. (defvar ange-ftp-gateway-program-interactive nil
  745.   "*If non-nil then the gateway program should  give a shell prompt.
  746. Both telnet and rlogin do something like this.")
  747.  
  748. (defvar ange-ftp-gateway-program remote-shell-program
  749.   "*Name of program to spawn a shell on the gateway machine.
  750. Valid candidates are rsh (remsh on some systems), telnet and rlogin.  See
  751. also the gateway variable above.")
  752.  
  753. (defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *"
  754.   "*Regexp matching prompt after complete login sequence on gateway machine.
  755. A match for this means the shell is now awaiting input.  Make this regexp as
  756. strict as possible; it shouldn't match *anything* at all except the user's
  757. initial prompt.  The above string will fail under most SUN-3's since it
  758. matches the login banner.")
  759.  
  760. (defvar ange-ftp-gateway-setup-term-command
  761.   (if (eq system-type 'hpux)
  762.       "stty -onlcr -echo\n"
  763.     "stty -echo nl\n")
  764.   "*Set up terminal after logging in to the gateway machine.
  765. This command should stop the terminal from echoing each command, and
  766. arrange to strip out trailing ^M characters.")
  767.  
  768. (defvar ange-ftp-smart-gateway nil
  769.   "*Non-nil means the ftp gateway and/or the gateway ftp program is smart.
  770. Don't bother telnetting, etc., already connected to desired host transparently,
  771. or just issue a user@host command in case \`ange-ftp-gateway-host\' is non-nil.")
  772.  
  773. (defvar ange-ftp-smart-gateway-port "21"
  774.   "*Port on gateway machine to use when smart gateway is in operation.")
  775.  
  776. (defvar ange-ftp-send-hash t
  777.   "*If non-nil, send the HASH command to the FTP client.")
  778.  
  779. (defvar ange-ftp-binary-hash-mark-size nil
  780.   "*Default size, in bytes, between hash-marks when transferring a binary file.
  781. If NIL, this variable will be locally overridden if the FTP client outputs a
  782. suitable response to the HASH command.  If non-NIL then this value takes
  783. precedence over the local value.")
  784.  
  785. (defvar ange-ftp-ascii-hash-mark-size 1024
  786.   "*Default size, in bytes, between hash-marks when transferring an ASCII file.
  787. This variable is buffer-local and will be locally overridden if the FTP client
  788. outputs a suitable response to the HASH command.")
  789.  
  790. (defvar ange-ftp-process-verbose t
  791.   "*If non-NIL then be chatty about interaction with the FTP process.")
  792.  
  793. (defvar ange-ftp-ftp-program-name "ftp"
  794.   "*Name of FTP program to run.")
  795.  
  796. (defvar ange-ftp-gateway-ftp-program-name "ftp"
  797.   "*Name of FTP program to run when accessing non-local hosts.
  798. Some AT&T folks claim to use something called `pftp' here.")
  799.  
  800. (defvar ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
  801.   "*A list of arguments passed to the FTP program when started.")
  802.  
  803. (defvar ange-ftp-nslookup-program nil
  804.   "*If non-NIL then a string naming nslookup program." )
  805.  
  806. (defvar ange-ftp-make-backup-files ()
  807.   "*Non-nil means make backup files for \"magic\" remote files.")
  808.  
  809. (defvar ange-ftp-retry-time 5
  810.   "*Number of seconds to wait before retry if file or listing doesn't arrive.
  811. This might need to be increased for very slow connections.")
  812.  
  813. (defvar ange-ftp-auto-save 0
  814.   "If 1, allows ange-ftp files to be auto-saved.
  815. If 0, suppresses auto-saving of ange-ftp files.
  816. Don't use any other value.")
  817.  
  818. ;;;; ------------------------------------------------------------
  819. ;;;; Hash table support.
  820. ;;;; ------------------------------------------------------------
  821.  
  822. (require 'backquote)
  823.  
  824. (defun ange-ftp-make-hashtable (&optional size)
  825.   "Make an obarray suitable for use as a hashtable.
  826. SIZE, if supplied, should be a prime number."
  827.   (make-vector (or size 31) 0))
  828.  
  829. (defun ange-ftp-map-hashtable (fun tbl)
  830.   "Call FUNCTION on each key and value in HASHTABLE."
  831.   (mapatoms
  832.    (function 
  833.     (lambda (sym)
  834.       (funcall fun (get sym 'key) (get sym 'val))))
  835.    tbl))
  836.  
  837. (defmacro ange-ftp-make-hash-key (key)
  838.   "Convert KEY into a suitable key for a hashtable."
  839.   (` (if (stringp (, key))
  840.      (, key)
  841.        (prin1-to-string (, key)))))
  842.  
  843. (defun ange-ftp-get-hash-entry (key tbl)
  844.   "Return the value associated with KEY in HASHTABLE."
  845.   (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl)))
  846.     (and sym (get sym 'val))))
  847.  
  848. (defun ange-ftp-put-hash-entry (key val tbl)
  849.   "Record an association between KEY and VALUE in HASHTABLE."
  850.   (let ((sym (intern (ange-ftp-make-hash-key key) tbl)))
  851.     (put sym 'val val)
  852.     (put sym 'key key)))
  853.  
  854. (defun ange-ftp-del-hash-entry (key tbl)
  855.   "Copy all symbols except KEY in HASHTABLE and return modified hashtable."
  856.   (let* ((len (length tbl))
  857.      (new-tbl (ange-ftp-make-hashtable len))
  858.      (i (1- len)))
  859.     (ange-ftp-map-hashtable
  860.      (function
  861.       (lambda (k v)
  862.     (or (equal k key)
  863.         (ange-ftp-put-hash-entry k v new-tbl))))
  864.      tbl)
  865.     (while (>= i 0)
  866.       (aset tbl i (aref new-tbl i))
  867.       (setq i (1- i)))
  868.     tbl))
  869.  
  870. (defun ange-ftp-hash-entry-exists-p (key tbl)
  871.   "Return whether there is an association for KEY in TABLE."
  872.   (intern-soft (ange-ftp-make-hash-key key) tbl))
  873.  
  874. (defun ange-ftp-hash-table-keys (tbl)
  875.   "Return a sorted list of all the active keys in TABLE, as strings."
  876.   (sort (all-completions "" tbl)
  877.     (function string-lessp)))
  878.  
  879. ;;;; ------------------------------------------------------------
  880. ;;;; Internal variables.
  881. ;;;; ------------------------------------------------------------
  882.  
  883. (defvar ange-ftp-data-buffer-name " *ftp data*"
  884.   "Buffer name to hold directory listing data received from ftp process.")
  885.  
  886. (defvar ange-ftp-netrc-modtime nil
  887.   "Last modified time of the netrc file from file-attributes.")
  888.  
  889. (defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable)
  890.   "Hash table holding associations between HOST, USER pairs.")
  891.  
  892. (defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable)
  893.   "Mapping between a HOST, USER pair and a PASSWORD for them.")
  894.  
  895. (defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable)
  896.   "Mapping between a HOST, USER pair and a ACCOUNT password for them.")
  897.  
  898. (defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97)
  899.   "Hash table for storing directories and their respective files.")
  900.  
  901. (defvar ange-ftp-ls-cache-lsargs nil
  902.   "Last set of args used by ange-ftp-ls.")
  903.  
  904. (defvar ange-ftp-ls-cache-file nil
  905.   "Last file passed to ange-ftp-ls.")
  906.  
  907. (defvar ange-ftp-ls-cache-res nil
  908.   "Last result returned from ange-ftp-ls.")
  909.  
  910. (defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable))
  911.  
  912. (defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")
  913.  
  914. ;; These are local variables in each FTP process buffer.
  915. (defvar ange-ftp-hash-mark-unit nil)
  916. (defvar ange-ftp-hash-mark-count nil)
  917. (defvar ange-ftp-xfer-size nil)
  918. (defvar ange-ftp-process-string nil)
  919. (defvar ange-ftp-process-result-line nil)
  920. (defvar ange-ftp-process-busy nil)
  921. (defvar ange-ftp-process-result nil)
  922. (defvar ange-ftp-process-multi-skip nil)
  923. (defvar ange-ftp-process-msg nil)
  924. (defvar ange-ftp-process-continue nil)
  925. (defvar ange-ftp-last-percent nil)
  926.  
  927. ;; These variables are bound by one function and examined by another.
  928. ;; Leave them void globally for error checking.
  929. (defvar ange-ftp-this-file)
  930. (defvar ange-ftp-this-dir)
  931. (defvar ange-ftp-this-user)
  932. (defvar ange-ftp-this-host)
  933. (defvar ange-ftp-this-msg)
  934. (defvar ange-ftp-completion-ignored-pattern)
  935. (defvar ange-ftp-trample-marker)
  936.  
  937. ;; New error symbols.
  938. (put 'ftp-error 'error-conditions '(ftp-error file-error error))
  939. ;; (put 'ftp-error 'error-message "FTP error")
  940.  
  941. ;;; ------------------------------------------------------------
  942. ;;; Enhanced message support.
  943. ;;; ------------------------------------------------------------
  944.  
  945. (defun ange-ftp-message (fmt &rest args)
  946.   "Display message in echo area, but indicate if truncated.
  947. Args are as in `message': a format string, plus arguments to be formatted."
  948.   (let ((msg (apply (function format) fmt args))
  949.     (max (window-width (minibuffer-window))))
  950.     (if noninteractive
  951.     msg
  952.       (if (>= (length msg) max)
  953.       ;; Take just the last MAX - 3 chars of the string.
  954.       (setq msg (concat "> " (substring msg (- 3 max)))))
  955.       (message "%s" msg))))
  956.  
  957. (defun ange-ftp-abbreviate-filename (file &optional new)
  958.   "Abbreviate the file name FILE relative to the default-directory.
  959. If the optional parameter NEW is given and the non-directory parts match,
  960. only return the directory part of FILE."
  961.   (save-match-data
  962.     (if (and default-directory
  963.          (string-match (concat "^"
  964.                    (regexp-quote default-directory)
  965.                    ".") file))
  966.     (setq file (substring file (1- (match-end 0)))))
  967.     (if (and new
  968.          (string-equal (file-name-nondirectory file)
  969.                (file-name-nondirectory new)))
  970.     (setq file (file-name-directory file)))
  971.     (or file "./")))
  972.  
  973. ;;;; ------------------------------------------------------------
  974. ;;;; User / Host mapping support.
  975. ;;;; ------------------------------------------------------------
  976.  
  977. (defun ange-ftp-set-user (host user)
  978.   "For a given HOST, set or change the default USER."
  979.   (interactive "sHost: \nsUser: ")
  980.   (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable))
  981.  
  982. (defun ange-ftp-get-user (host)
  983.   "Given a HOST, return the default USER."
  984.   (ange-ftp-parse-netrc)
  985.   (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable)))
  986.     (or user
  987.     (prog1
  988.         (setq user
  989.           (cond ((stringp ange-ftp-default-user)
  990.              ;; We have a default name.  Use it.
  991.              ange-ftp-default-user)
  992.             (ange-ftp-default-user
  993.              ;; Ask the user.
  994.              (let ((enable-recursive-minibuffers t))
  995.                (read-string (format "User for %s: " host)
  996.                     (user-login-name))))
  997.             (ange-ftp-netrc-default-user)
  998.             ;; Default to the user's login name.
  999.             (t
  1000.              (user-login-name))))
  1001.       (ange-ftp-set-user host user)))))
  1002.  
  1003. ;;;; ------------------------------------------------------------
  1004. ;;;; Password support.
  1005. ;;;; ------------------------------------------------------------
  1006.  
  1007. (defun ange-ftp-read-passwd (prompt &optional default)
  1008.   "Read a password, echoing `.' for each character typed.
  1009. End with RET, LFD, or ESC.  DEL or C-h rubs out.  C-u kills line.
  1010. Optional DEFAULT is password to start with."
  1011.   (let ((pass nil)
  1012.     (c 0)
  1013.     (echo-keystrokes 0)
  1014.     (cursor-in-echo-area t))
  1015.     (while (progn (message "%s%s"
  1016.                prompt
  1017.                (make-string (length pass) ?.))
  1018.           (setq c (read-char))
  1019.           (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
  1020.       (if (= c ?\C-u)
  1021.       (setq pass "")
  1022.     (if (and (/= c ?\b) (/= c ?\177))
  1023.         (setq pass (concat pass (char-to-string c)))
  1024.       (if (> (length pass) 0)
  1025.           (setq pass (substring pass 0 -1))))))
  1026.     (message "")
  1027.     (ange-ftp-repaint-minibuffer)
  1028.     (or pass default "")))
  1029.  
  1030. (defmacro ange-ftp-generate-passwd-key (host user)
  1031.   (` (concat (, host) "/" (, user))))
  1032.  
  1033. (defmacro ange-ftp-lookup-passwd (host user)
  1034.   (` (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key (, host) (, user))
  1035.                   ange-ftp-passwd-hashtable)))
  1036.  
  1037. (defun ange-ftp-set-passwd (host user passwd)
  1038.   "For a given HOST and USER, set or change the associated PASSWORD."
  1039.   (interactive (list (read-string "Host: ")
  1040.              (read-string "User: ")
  1041.              (ange-ftp-read-passwd "Password: ")))
  1042.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1043.                passwd
  1044.                ange-ftp-passwd-hashtable))
  1045.  
  1046. (defun ange-ftp-get-host-with-passwd (user)
  1047.   "Given a USER, return a host we know the password for."
  1048.   (ange-ftp-parse-netrc)
  1049.   (catch 'found-one
  1050.     (ange-ftp-map-hashtable
  1051.      (function (lambda (host val)
  1052.          (if (ange-ftp-lookup-passwd host user)
  1053.              (throw 'found-one host))))
  1054.      ange-ftp-user-hashtable)
  1055.     (save-match-data
  1056.       (ange-ftp-map-hashtable
  1057.        (function
  1058.     (lambda (key value)
  1059.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1060.           (let ((host (substring key 0 (match-beginning 1))))
  1061.         (if (and (string-equal user (substring key (match-end 1)))
  1062.              value)
  1063.             (throw 'found-one host))))))
  1064.        ange-ftp-passwd-hashtable))
  1065.     nil))
  1066.  
  1067. (defun ange-ftp-get-passwd (host user)
  1068.   "Return the password for specified HOST and USER, asking user if necessary."
  1069.   (ange-ftp-parse-netrc)
  1070.  
  1071.   ;; look up password in the hash table first; user might have overridden the
  1072.   ;; defaults.
  1073.   (cond ((ange-ftp-lookup-passwd host user))
  1074.     
  1075.     ;; See if default user and password set.
  1076.     ((and (stringp ange-ftp-default-user)
  1077.           ange-ftp-default-password
  1078.           (string-equal user ange-ftp-default-user))
  1079.      ange-ftp-default-password)
  1080.     
  1081.     ;; See if default user and password set from .netrc file.
  1082.     ((and (stringp ange-ftp-netrc-default-user)
  1083.           ange-ftp-netrc-default-password
  1084.           (string-equal user ange-ftp-netrc-default-user))
  1085.      ange-ftp-netrc-default-password)
  1086.     
  1087.     ;; anonymous ftp password is handled specially since there is an
  1088.     ;; unwritten rule about how that is used on the Internet.
  1089.     ((and (or (string-equal user "anonymous")
  1090.           (string-equal user "ftp"))
  1091.           ange-ftp-generate-anonymous-password)
  1092.      (if (stringp ange-ftp-generate-anonymous-password)
  1093.          ange-ftp-generate-anonymous-password
  1094.        user-mail-address))
  1095.     
  1096.     ;; see if same user has logged in to other hosts; if so then prompt
  1097.     ;; with the password that was used there.
  1098.     (t
  1099.      (let* ((other (ange-ftp-get-host-with-passwd user))
  1100.         (passwd (if other
  1101.                 
  1102.                 ;; found another machine with the same user.
  1103.                 ;; Try that account.
  1104.                 (ange-ftp-read-passwd
  1105.                  (format "passwd for %s@%s (default same as %s@%s): "
  1106.                      user host user other)
  1107.                  (ange-ftp-lookup-passwd other user))
  1108.               
  1109.               ;; I give up.  Ask the user for the password.
  1110.               (ange-ftp-read-passwd
  1111.                (format "Password for %s@%s: " user host)))))
  1112.        (ange-ftp-set-passwd host user passwd)
  1113.        passwd))))
  1114.  
  1115. ;;;; ------------------------------------------------------------
  1116. ;;;; Account support
  1117. ;;;; ------------------------------------------------------------
  1118.  
  1119. ;; Account passwords must be either specified in the .netrc file, or set
  1120. ;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
  1121. ;; check to see whether the FTP process is actually prompting for an account
  1122. ;; password.
  1123.  
  1124. (defun ange-ftp-set-account (host user account)
  1125.   "For a given HOST and USER, set or change the associated ACCOUNT password."
  1126.   (interactive (list (read-string "Host: ")
  1127.              (read-string "User: ")
  1128.              (ange-ftp-read-passwd "Account password: ")))
  1129.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1130.                account
  1131.                ange-ftp-account-hashtable))
  1132.  
  1133. (defun ange-ftp-get-account (host user)
  1134.   "Given a HOST and USER, return the FTP account."
  1135.   (ange-ftp-parse-netrc)
  1136.   (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user)
  1137.                    ange-ftp-account-hashtable)
  1138.       (and (stringp ange-ftp-default-user)
  1139.        (string-equal user ange-ftp-default-user)
  1140.        ange-ftp-default-account)
  1141.       (and (stringp ange-ftp-netrc-default-user)
  1142.        (string-equal user ange-ftp-netrc-default-user)
  1143.        ange-ftp-netrc-default-account)))
  1144.  
  1145. ;;;; ------------------------------------------------------------
  1146. ;;;; ~/.netrc support
  1147. ;;;; ------------------------------------------------------------
  1148.  
  1149. (defun ange-ftp-chase-symlinks (file)
  1150.   "Return the filename that FILE references, following all symbolic links."
  1151.   (let (temp)
  1152.     (while (setq temp (ange-ftp-real-file-symlink-p file))
  1153.       (setq file
  1154.         (if (file-name-absolute-p temp)
  1155.         temp
  1156.           (concat (file-name-directory file) temp)))))
  1157.   file)
  1158.  
  1159. ;; Move along current line looking for the value of the TOKEN.
  1160. ;; Valid separators between TOKEN and its value are commas and
  1161. ;; whitespace.  Second arg LIMIT is a limit for the search.
  1162.  
  1163. (defun ange-ftp-parse-netrc-token (token limit)
  1164.   (if (search-forward token limit t)
  1165.       (let (beg)
  1166.     (skip-chars-forward ", \t\r\n" limit)
  1167.     (if (eq (following-char) ?\")    ;quoted token value
  1168.         (progn (forward-char 1)
  1169.            (setq beg (point))
  1170.            (skip-chars-forward "^\"" limit)
  1171.            (forward-char 1)
  1172.            (buffer-substring beg (1- (point))))
  1173.       (setq beg (point))
  1174.       (skip-chars-forward "^, \t\r\n" limit)
  1175.       (buffer-substring beg (point))))))
  1176.  
  1177. ;; Extract the values for the tokens `machine', `login',
  1178. ;; `password' and `account' in the current buffer.  If successful,
  1179. ;; record the information found.
  1180.  
  1181. (defun ange-ftp-parse-netrc-group ()
  1182.   (let ((start (point))
  1183.     (end (save-excursion
  1184.            (if (looking-at "machine\\>")
  1185.            ;; Skip `machine' and the machine name that follows.
  1186.            (progn
  1187.              (skip-chars-forward "^ \t\n")
  1188.              (skip-chars-forward " \t\n")
  1189.              (skip-chars-forward "^ \t\n"))
  1190.          ;; Skip `default'.
  1191.          (skip-chars-forward "^ \t\n"))
  1192.            ;; Find start of the next `machine' or `default'
  1193.            ;; or the end of the buffer.
  1194.            (if (re-search-forward "machine\\>\\|default\\>" nil t)
  1195.            (match-beginning 0)
  1196.          (point-max))))
  1197.     machine login password account)
  1198.     (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
  1199.       login    (ange-ftp-parse-netrc-token "login"    end)
  1200.       password (ange-ftp-parse-netrc-token "password" end)
  1201.       account  (ange-ftp-parse-netrc-token "account"  end))
  1202.     (if (and machine login)
  1203.     ;; found a `machine` token.
  1204.     (progn
  1205.       (ange-ftp-set-user machine login)
  1206.       (ange-ftp-set-passwd machine login password)
  1207.       (and account
  1208.            (ange-ftp-set-account machine login account)))
  1209.       (goto-char start)
  1210.       (if (search-forward "default" end t)
  1211.       ;; found a `default' token
  1212.       (progn
  1213.         (setq login    (ange-ftp-parse-netrc-token "login"    end)
  1214.           password (ange-ftp-parse-netrc-token "password" end)
  1215.           account  (ange-ftp-parse-netrc-token "account"  end))
  1216.         (and login
  1217.          (setq ange-ftp-netrc-default-user login))
  1218.         (and password
  1219.          (setq ange-ftp-netrc-default-password password))
  1220.         (and account
  1221.          (setq ange-ftp-netrc-default-account account)))))
  1222.     (goto-char end)))
  1223.  
  1224. ;; Read in ~/.netrc, if one exists.  If ~/.netrc file exists and has
  1225. ;; the correct permissions then extract the \`machine\', \`login\',
  1226. ;; \`password\' and \`account\' information from within.
  1227.  
  1228. (defun ange-ftp-parse-netrc ()
  1229.   ;; We set this before actually doing it to avoid the possibility
  1230.   ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  1231.   (interactive)
  1232.   (let (file attr)
  1233.     (let ((default-directory "/"))
  1234.       (setq file (ange-ftp-chase-symlinks
  1235.           (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
  1236.       (setq attr (ange-ftp-real-file-attributes file)))
  1237.     (if (and attr            ; file exists.
  1238.          (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))    ; file changed
  1239.     (save-match-data
  1240.       (if (or ange-ftp-disable-netrc-security-check
  1241.           (and (eq (nth 2 attr) (user-uid)) ; Same uids.
  1242.                (string-match ".r..------" (nth 8 attr))))
  1243.           (save-excursion
  1244.         ;; we are cheating a bit here.  I'm trying to do the equivalent
  1245.         ;; of find-file on the .netrc file, but then nuke it afterwards.
  1246.         ;; with the bit of logic below we should be able to have
  1247.         ;; encrypted .netrc files.
  1248.         (set-buffer (generate-new-buffer "*ftp-.netrc*"))
  1249.         (ange-ftp-real-insert-file-contents file)
  1250.         (setq buffer-file-name file)
  1251.         (setq default-directory (file-name-directory file))
  1252.         (normal-mode t)
  1253.         (mapcar 'funcall find-file-hooks)
  1254.         (setq buffer-file-name nil)
  1255.         (goto-char (point-min))
  1256.         (skip-chars-forward " \t\n")
  1257.         (while (not (eobp))
  1258.           (ange-ftp-parse-netrc-group))
  1259.         (kill-buffer (current-buffer)))
  1260.         (ange-ftp-message "%s either not owned by you or badly protected."
  1261.                   ange-ftp-netrc-filename)
  1262.         (sit-for 1))
  1263.       (setq ange-ftp-netrc-modtime (nth 5 attr))))))
  1264.  
  1265. ;; Return a list of prefixes of the form 'user@host:' to be used when
  1266. ;; completion is done in the root directory.
  1267.  
  1268. (defun ange-ftp-generate-root-prefixes ()
  1269.   (ange-ftp-parse-netrc)
  1270.   (save-match-data
  1271.     (let (res)
  1272.       (ange-ftp-map-hashtable
  1273.        (function
  1274.     (lambda (key value)
  1275.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1276.           (let ((host (substring key 0 (match-beginning 1)))
  1277.             (user (substring key (match-end 1))))
  1278.         (setq res (cons (list (concat user "@" host ":"))
  1279.                 res))))))
  1280.        ange-ftp-passwd-hashtable)
  1281.       (ange-ftp-map-hashtable
  1282.        (function (lambda (host user)
  1283.            (setq res (cons (list (concat host ":"))
  1284.                    res))))
  1285.        ange-ftp-user-hashtable)
  1286.       (or res (list nil)))))
  1287.  
  1288. ;;;; ------------------------------------------------------------
  1289. ;;;; Remote file name syntax support.
  1290. ;;;; ------------------------------------------------------------
  1291.  
  1292. (defmacro ange-ftp-ftp-name-component (n ns name)
  1293.   "Extract the Nth ftp file name component from NS."
  1294.   (` (let ((elt (nth (, n) (, ns))))
  1295.        (if (match-beginning elt)
  1296.        (substring (, name) (match-beginning elt) (match-end elt))))))
  1297.  
  1298. (defvar ange-ftp-ftp-name-arg "")
  1299. (defvar ange-ftp-ftp-name-res nil)
  1300.  
  1301. ;; Parse NAME according to `ange-ftp-name-format' (which see).
  1302. ;; Returns a list (HOST USER NAME), or nil if NAME does not match the format.
  1303. (defun ange-ftp-ftp-name (name)
  1304.   (if (string-equal name ange-ftp-ftp-name-arg)
  1305.       ange-ftp-ftp-name-res
  1306.     (setq ange-ftp-ftp-name-arg name
  1307.       ange-ftp-ftp-name-res
  1308.       (save-match-data
  1309.         (if (posix-string-match (car ange-ftp-name-format) name)
  1310.         (let* ((ns (cdr ange-ftp-name-format))
  1311.                (host (ange-ftp-ftp-name-component 0 ns name))
  1312.                (user (ange-ftp-ftp-name-component 1 ns name))
  1313.                (name (ange-ftp-ftp-name-component 2 ns name)))
  1314.           (if (zerop (length user))
  1315.               (setq user (ange-ftp-get-user host)))
  1316.           (list host user name))
  1317.           nil)))))
  1318.  
  1319. ;; Take a FULLNAME that matches according to ange-ftp-name-format and
  1320. ;; replace the name component with NAME.
  1321. (defun ange-ftp-replace-name-component (fullname name)
  1322.   (save-match-data
  1323.     (if (posix-string-match (car ange-ftp-name-format) fullname)
  1324.     (let* ((ns (cdr ange-ftp-name-format))
  1325.            (elt (nth 2 ns)))
  1326.       (concat (substring fullname 0 (match-beginning elt))
  1327.           name
  1328.           (substring fullname (match-end elt)))))))
  1329.  
  1330. ;;;; ------------------------------------------------------------
  1331. ;;;; Miscellaneous utils.
  1332. ;;;; ------------------------------------------------------------
  1333.  
  1334. ;; (setq ange-ftp-tmp-keymap (make-sparse-keymap))
  1335. ;; (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)
  1336.  
  1337. (defun ange-ftp-repaint-minibuffer ()
  1338.   "Clear any existing minibuffer message; let the minibuffer contents show."
  1339.   (message nil))
  1340.  
  1341. ;; Return the name of the buffer that collects output from the ftp process
  1342. ;; connected to the given HOST and USER pair.
  1343. (defun ange-ftp-ftp-process-buffer (host user)
  1344.   (concat "*ftp " user "@" host "*"))
  1345.  
  1346. ;; Display the last chunk of output from the ftp process for the given HOST
  1347. ;; USER pair, and signal an error including MSG in the text.
  1348. (defun ange-ftp-error (host user msg)
  1349.   (let ((cur (selected-window))
  1350.     (pop-up-windows t))
  1351.     (pop-to-buffer
  1352.      (get-buffer-create
  1353.       (ange-ftp-ftp-process-buffer host user)))
  1354.     (goto-char (point-max))
  1355.     (select-window cur))
  1356.   (signal 'ftp-error (list (format "FTP Error: %s" msg))))
  1357.  
  1358. (defun ange-ftp-set-buffer-mode ()
  1359.   "Set correct modes for the current buffer if visiting a remote file."
  1360.   (if (and (stringp buffer-file-name)
  1361.        (ange-ftp-ftp-name buffer-file-name))
  1362.       (auto-save-mode ange-ftp-auto-save)))
  1363.  
  1364. (defun ange-ftp-kill-ftp-process (&optional buffer)
  1365.   "Kill the FTP process associated with BUFFER (the current buffer, if nil).
  1366. If the BUFFER's visited filename or default-directory is an ftp filename
  1367. then kill the related ftp process."
  1368.   (interactive "bKill FTP process associated with buffer: ")
  1369.   (if (null buffer)
  1370.       (setq buffer (current-buffer)))
  1371.   (let ((file (or (buffer-file-name buffer)
  1372.           (save-excursion (set-buffer buffer) default-directory))))
  1373.     (if file
  1374.     (let ((parsed (ange-ftp-ftp-name (expand-file-name file))))
  1375.       (if parsed
  1376.           (let ((host (nth 0 parsed))
  1377.             (user (nth 1 parsed)))
  1378.         (kill-buffer (ange-ftp-ftp-process-buffer host user))))))))
  1379.  
  1380. (defun ange-ftp-quote-string (string)
  1381.   "Quote any characters in STRING that may confuse the ftp process."
  1382.   (apply (function concat)
  1383.      (mapcar (function
  1384.            (lambda (char)
  1385.              (if (or (<= char ? )
  1386.                  (> char ?\~)
  1387.                      (= char ?\")
  1388.                  (= char ?\\))
  1389.              (vector ?\\ char)
  1390.                (vector char))))
  1391.          string)))
  1392.  
  1393. (defun ange-ftp-barf-if-not-directory (directory)
  1394.   (or (file-directory-p directory)
  1395.       (signal 'file-error
  1396.           (list "Opening directory"
  1397.             (if (file-exists-p directory)
  1398.             "not a directory"
  1399.               "no such file or directory")
  1400.             directory))))
  1401.  
  1402. ;;;; ------------------------------------------------------------
  1403. ;;;; FTP process filter support.
  1404. ;;;; ------------------------------------------------------------
  1405.  
  1406. (defun ange-ftp-process-handle-line (line proc)
  1407.   "Look at the given LINE from the ftp process PROC.
  1408. Try to categorize it into one of four categories:
  1409. good, skip, fatal, or unknown."
  1410.   (cond ((string-match ange-ftp-xfer-size-msgs line)
  1411.      (setq ange-ftp-xfer-size
  1412.            (ash (string-to-int (substring line
  1413.                           (match-beginning 1)
  1414.                           (match-end 1)))
  1415.             -10)))
  1416.     ((string-match ange-ftp-skip-msgs line)
  1417.      t)
  1418.     ((string-match ange-ftp-good-msgs line)
  1419.      (setq ange-ftp-process-busy nil
  1420.            ange-ftp-process-result t
  1421.            ange-ftp-process-result-line line))
  1422.     ;; Check this before checking for errors.
  1423.     ;; Otherwise the last line of these three seems to be an error:
  1424.     ;; 230-see a significant impact from the move.  For those of you who can't
  1425.     ;; 230-use DNS to resolve hostnames and get an error message like
  1426.     ;; 230-"ftp.stsci.edu: unknown host", the new IP address will be...
  1427.     ((string-match ange-ftp-multi-msgs line)
  1428.      (setq ange-ftp-process-multi-skip t))
  1429.     ((string-match ange-ftp-fatal-msgs line)
  1430.      (delete-process proc)
  1431.      (setq ange-ftp-process-busy nil
  1432.            ange-ftp-process-result-line line))
  1433.     (ange-ftp-process-multi-skip
  1434.      t)
  1435.     (t
  1436.      (setq ange-ftp-process-busy nil
  1437.            ange-ftp-process-result-line line))))
  1438.  
  1439. (defun ange-ftp-set-xfer-size (host user bytes)
  1440.   "Set the size of the next FTP transfer in bytes."
  1441.   (let ((proc (ange-ftp-get-process host user)))
  1442.     (if proc
  1443.     (let ((buf (process-buffer proc)))
  1444.       (if buf
  1445.           (save-excursion
  1446.         (set-buffer buf)
  1447.         (setq ange-ftp-xfer-size (ash bytes -10))))))))
  1448.  
  1449. (defun ange-ftp-process-handle-hash (str)
  1450.   "Remove hash marks from STRING and display count so far."
  1451.   (setq str (concat (substring str 0 (match-beginning 0))
  1452.             (substring str (match-end 0)))
  1453.     ange-ftp-hash-mark-count (+ (- (match-end 0)
  1454.                        (match-beginning 0))
  1455.                     ange-ftp-hash-mark-count))
  1456.   (and ange-ftp-hash-mark-unit
  1457.        ange-ftp-process-msg
  1458.        ange-ftp-process-verbose
  1459.        (not (eq (selected-window) (minibuffer-window)))
  1460.        (not (boundp 'search-message))    ;screws up isearch otherwise
  1461.        (not cursor-in-echo-area)    ;screws up y-or-n-p otherwise
  1462.        (let ((kbytes (ash (* ange-ftp-hash-mark-unit
  1463.                  ange-ftp-hash-mark-count)
  1464.               -6)))
  1465.        (if (zerop ange-ftp-xfer-size)
  1466.        (ange-ftp-message "%s...%dk" ange-ftp-process-msg kbytes)
  1467.      (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size)))
  1468.        ;; cut out the redisplay of identical %-age messages.
  1469.        (if (not (eq percent ange-ftp-last-percent))
  1470.            (progn
  1471.          (setq ange-ftp-last-percent percent)
  1472.          (ange-ftp-message "%s...%d%%" ange-ftp-process-msg percent)))))))
  1473.   str)
  1474.  
  1475. ;; Call the function specified by CONT.  CONT can be either a function
  1476. ;; or a list of a function and some args.  The first two parameters
  1477. ;; passed to the function will be RESULT and LINE.  The remaining args
  1478. ;; will be taken from CONT if a list was passed.
  1479.  
  1480. (defun ange-ftp-call-cont (cont result line)
  1481.   (if cont
  1482.       (if (and (listp cont)
  1483.            (not (eq (car cont) 'lambda)))
  1484.       (apply (car cont) result line (cdr cont))
  1485.     (funcall cont result line))))
  1486.  
  1487. ;; Build up a complete line of output from the ftp PROCESS and pass it
  1488. ;; on to ange-ftp-process-handle-line to deal with.
  1489.  
  1490. (defun ange-ftp-process-filter (proc str)
  1491.   (let ((buffer (process-buffer proc))
  1492.     (old-buffer (current-buffer)))
  1493.  
  1494.     ;; see if the buffer is still around... it could have been deleted.
  1495.     (if (buffer-name buffer)
  1496.     (unwind-protect
  1497.         (progn
  1498.           (set-buffer (process-buffer proc))
  1499.           
  1500.           ;; handle hash mark printing
  1501.           (and ange-ftp-process-busy
  1502.            (string-match "^#+$" str)
  1503.            (setq str (ange-ftp-process-handle-hash str)))
  1504.           (comint-output-filter proc str)
  1505.           ;; Replace STR by the result of the comint processing.
  1506.           (setq str (buffer-substring comint-last-output-start
  1507.                       (process-mark proc)))
  1508.           (if ange-ftp-process-busy
  1509.           (progn
  1510.             (setq ange-ftp-process-string (concat ange-ftp-process-string
  1511.                               str))
  1512.             
  1513.             ;; if we gave an empty password to the USER command earlier
  1514.             ;; then we should send a null password now.
  1515.             (if (string-match "Password: *$" ange-ftp-process-string)
  1516.             (send-string proc "\n"))))
  1517.           (while (and ange-ftp-process-busy
  1518.               (string-match "\n" ange-ftp-process-string))
  1519.         (let ((line (substring ange-ftp-process-string
  1520.                        0
  1521.                        (match-beginning 0))))
  1522.           (setq ange-ftp-process-string (substring ange-ftp-process-string
  1523.                                (match-end 0)))
  1524.           (while (string-match "^ftp> *" line)
  1525.             (setq line (substring line (match-end 0))))
  1526.           (ange-ftp-process-handle-line line proc)))
  1527.  
  1528.           ;; has the ftp client finished?  if so then do some clean-up
  1529.           ;; actions.
  1530.           (if (not ange-ftp-process-busy)
  1531.           (progn
  1532.             ;; reset the xfer size
  1533.             (setq ange-ftp-xfer-size 0)
  1534.  
  1535.             ;; issue the "done" message since we've finished.
  1536.             (if (and ange-ftp-process-msg
  1537.                  ange-ftp-process-verbose
  1538.                  ange-ftp-process-result)
  1539.             (progn
  1540.               (ange-ftp-message "%s...done" ange-ftp-process-msg)
  1541.               (ange-ftp-repaint-minibuffer)
  1542.               (setq ange-ftp-process-msg nil)))
  1543.             
  1544.             ;; is there a continuation we should be calling?  if so,
  1545.             ;; we'd better call it, making sure we only call it once.
  1546.             (if ange-ftp-process-continue
  1547.             (let ((cont ange-ftp-process-continue))
  1548.               (setq ange-ftp-process-continue nil)
  1549.               (ange-ftp-call-cont cont
  1550.                           ange-ftp-process-result
  1551.                           ange-ftp-process-result-line))))))
  1552.       (set-buffer old-buffer)))))
  1553.  
  1554. (defun ange-ftp-process-sentinel (proc str)
  1555.   "When ftp process changes state, nuke all file-entries in cache."
  1556.   (let ((name (process-name proc)))
  1557.     (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)\\*" name)
  1558.     (let ((user (substring name (match-beginning 1) (match-end 1)))
  1559.           (host (substring name (match-beginning 2) (match-end 2))))
  1560.       (ange-ftp-wipe-file-entries host user))))
  1561.   (setq ange-ftp-ls-cache-file nil))
  1562.  
  1563. ;;;; ------------------------------------------------------------
  1564. ;;;; Gateway support.
  1565. ;;;; ------------------------------------------------------------
  1566.  
  1567. (defun ange-ftp-use-gateway-p (host)
  1568.   "Returns whether to access this host via a normal (non-smart) gateway."
  1569.   ;; yes, I know that I could simplify the following expression, but it is
  1570.   ;; clearer (to me at least) this way.
  1571.   (and (not ange-ftp-smart-gateway)
  1572.        (save-match-data
  1573.      (not (string-match ange-ftp-local-host-regexp host)))))
  1574.  
  1575. (defun ange-ftp-use-smart-gateway-p (host)
  1576.   "Returns whether to access this host via a smart gateway."
  1577.   (and ange-ftp-smart-gateway
  1578.        (save-match-data
  1579.      (not (string-match ange-ftp-local-host-regexp host)))))
  1580.  
  1581.  
  1582. ;;; ------------------------------------------------------------
  1583. ;;; Temporary file location and deletion...
  1584. ;;; ------------------------------------------------------------
  1585.  
  1586. (defvar ange-ftp-tmp-name-files ())
  1587. (defvar ange-ftp-tmp-name-hashtable (ange-ftp-make-hashtable 10))
  1588. (defvar ange-ftp-pid nil)
  1589.  
  1590. (defun ange-ftp-get-pid ()
  1591.   "Half-hearted attempt to get the current process's id."
  1592.   (setq ange-ftp-pid (substring (make-temp-name "") 1)))
  1593.  
  1594. (defun ange-ftp-make-tmp-name (host)
  1595.   "This routine will return the name of a new file."
  1596.   (let* ((template (if (ange-ftp-use-gateway-p host)
  1597.                ange-ftp-gateway-tmp-name-template
  1598.              ange-ftp-tmp-name-template))
  1599.      (pid (or ange-ftp-pid (ange-ftp-get-pid)))
  1600.      (start ?a)
  1601.      file entry)
  1602.     (while 
  1603.     (progn
  1604.       (setq file (format "%s%c%s" template start pid))
  1605.       (setq entry (intern file ange-ftp-tmp-name-hashtable))
  1606.       (or (memq entry ange-ftp-tmp-name-files)
  1607.           (ange-ftp-real-file-exists-p file)))
  1608.       (if (> (setq start (1+ start)) ?z)
  1609.       (progn
  1610.         (setq template (concat template "X"))
  1611.         (setq start ?a))))
  1612.     (setq ange-ftp-tmp-name-files
  1613.       (cons entry ange-ftp-tmp-name-files))
  1614.     file))
  1615.  
  1616. (defun ange-ftp-del-tmp-name (temp)
  1617.   (setq ange-ftp-tmp-name-files
  1618.     (delq (intern temp ange-ftp-tmp-name-hashtable)
  1619.           ange-ftp-tmp-name-files))
  1620.   (condition-case ()
  1621.       (ange-ftp-real-delete-file temp)
  1622.     (error nil)))
  1623.  
  1624. ;;;; ------------------------------------------------------------
  1625. ;;;; Interactive gateway program support.
  1626. ;;;; ------------------------------------------------------------
  1627.  
  1628. (defvar ange-ftp-gwp-running t)
  1629. (defvar ange-ftp-gwp-status nil)
  1630.  
  1631. (defun ange-ftp-gwp-sentinel (proc str)
  1632.   (setq ange-ftp-gwp-running nil))
  1633.  
  1634. (defun ange-ftp-gwp-filter (proc str)
  1635.   (comint-output-filter proc str)
  1636.   (save-excursion
  1637.     (set-buffer (process-buffer proc))
  1638.     ;; Replace STR by the result of the comint processing.
  1639.     (setq str (buffer-substring comint-last-output-start (process-mark proc))))
  1640.   (cond ((string-match "login: *$" str)
  1641.      (send-string proc
  1642.               (concat
  1643.                (let ((ange-ftp-default-user t))
  1644.              (ange-ftp-get-user ange-ftp-gateway-host))
  1645.                "\n")))
  1646.     ((string-match "Password: *$" str)
  1647.      (send-string proc
  1648.               (concat
  1649.                (ange-ftp-get-passwd ange-ftp-gateway-host
  1650.                         (ange-ftp-get-user
  1651.                          ange-ftp-gateway-host))
  1652.                "\n")))
  1653.     ((string-match ange-ftp-gateway-fatal-msgs str)
  1654.      (delete-process proc)
  1655.      (setq ange-ftp-gwp-running nil))
  1656.     ((string-match ange-ftp-gateway-prompt-pattern str)
  1657.      (setq ange-ftp-gwp-running nil
  1658.            ange-ftp-gwp-status t))))
  1659.  
  1660. (defun ange-ftp-gwp-start (host user name args)
  1661.   "Login to the gateway machine and fire up an ftp process."
  1662.   (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host))
  1663.      ;; It would be nice to make process-connection-type nil,
  1664.      ;; but that doesn't work: ftp never responds.
  1665.      ;; Can anyone find a fix for that?
  1666.      (proc (let ((process-connection-type t))
  1667.          (start-process name name
  1668.                 ange-ftp-gateway-program
  1669.                 ange-ftp-gateway-host)))
  1670.      (ftp (mapconcat (function identity) args " ")))
  1671.     (process-kill-without-query proc)
  1672.     (set-process-sentinel proc (function ange-ftp-gwp-sentinel))
  1673.     (set-process-filter proc (function ange-ftp-gwp-filter))
  1674.     (save-excursion
  1675.       (set-buffer (process-buffer proc))
  1676.       (internal-ange-ftp-mode)
  1677.       (set-marker (process-mark proc) (point)))
  1678.     (setq ange-ftp-gwp-running t
  1679.       ange-ftp-gwp-status nil)
  1680.     (ange-ftp-message "Connecting to gateway %s..." ange-ftp-gateway-host)
  1681.     (while ange-ftp-gwp-running        ;perform login sequence
  1682.       (accept-process-output proc))
  1683.     (if (not ange-ftp-gwp-status)
  1684.     (ange-ftp-error host user "unable to login to gateway"))
  1685.     (ange-ftp-message "Connecting to gateway %s...done" ange-ftp-gateway-host)
  1686.     (setq ange-ftp-gwp-running t
  1687.       ange-ftp-gwp-status nil)
  1688.     (process-send-string proc ange-ftp-gateway-setup-term-command)
  1689.     (while ange-ftp-gwp-running        ;zap ^M's and double echoing.
  1690.       (accept-process-output proc))
  1691.     (if (not ange-ftp-gwp-status)
  1692.     (ange-ftp-error host user "unable to set terminal modes on gateway"))
  1693.     (setq ange-ftp-gwp-running t
  1694.       ange-ftp-gwp-status nil)
  1695.     (process-send-string proc (concat "exec " ftp "\n")) ;spawn ftp process
  1696.     proc))
  1697.  
  1698. ;;;; ------------------------------------------------------------
  1699. ;;;; Support for sending commands to the ftp process.
  1700. ;;;; ------------------------------------------------------------
  1701.  
  1702. (defun ange-ftp-raw-send-cmd (proc cmd &optional msg cont nowait)
  1703.   "Low-level routine to send the given ftp CMD to the ftp PROCESS.
  1704. MSG is an optional message to output before and after the command.
  1705. If CONT is non-NIL then it is either a function or a list of function and
  1706. some arguments.  The function will be called when the ftp command has completed.
  1707. If CONT is NIL then this routine will return \( RESULT . LINE \) where RESULT
  1708. is whether the command was successful, and LINE is the line from the FTP
  1709. process that caused the command to complete.
  1710. If NOWAIT is given then the routine will return immediately the command has
  1711. been queued with no result.  CONT will still be called, however."
  1712.   (if (memq (process-status proc) '(run open))
  1713.       (save-excursion
  1714.     (set-buffer (process-buffer proc))
  1715.     (ange-ftp-wait-not-busy proc)
  1716.     (setq ange-ftp-process-string ""
  1717.           ange-ftp-process-result-line ""
  1718.           ange-ftp-process-busy t
  1719.           ange-ftp-process-result nil
  1720.           ange-ftp-process-multi-skip nil
  1721.           ange-ftp-process-msg msg
  1722.           ange-ftp-process-continue cont
  1723.           ange-ftp-hash-mark-count 0
  1724.           ange-ftp-last-percent -1
  1725.           cmd (concat cmd "\n"))
  1726.     (and msg ange-ftp-process-verbose (ange-ftp-message "%s..." msg))
  1727.     (goto-char (point-max))
  1728.     (move-marker comint-last-input-start (point))
  1729.     ;; don't insert the password into the buffer on the USER command.
  1730.     (save-match-data
  1731.       (if (string-match "^user \"[^\"]*\"" cmd)
  1732.           (insert (substring cmd 0 (match-end 0)) " Turtle Power!\n")
  1733.         (insert cmd)))
  1734.     (move-marker comint-last-input-end (point))
  1735.     (send-string proc cmd)
  1736.     (set-marker (process-mark proc) (point))
  1737.     (if nowait
  1738.         nil
  1739.       (ange-ftp-wait-not-busy proc)
  1740.       (if cont
  1741.           nil            ;cont has already been called
  1742.         (cons ange-ftp-process-result ange-ftp-process-result-line))))))
  1743.  
  1744. ;; Wait for the ange-ftp process PROC not to be busy.
  1745. (defun ange-ftp-wait-not-busy (proc)
  1746.   (save-excursion
  1747.     (set-buffer (process-buffer proc))
  1748.     (condition-case nil
  1749.     ;; This is a kludge to let user quit in case ftp gets hung.
  1750.     ;; It matters because this function can be called from the filter.
  1751.     ;; It is bad to allow quitting in a filter, but getting hung
  1752.     ;; is worse.  By binding quit-flag to nil, we might avoid
  1753.     ;; most of the probability of getting screwed because the user
  1754.     ;; wants to quit some command.
  1755.     (let ((quit-flag nil)
  1756.           (inhibit-quit nil))
  1757.       (while ange-ftp-process-busy
  1758.         (accept-process-output proc)))
  1759.       (quit
  1760.        ;; If the user does quit out of this,
  1761.        ;; kill the process.  That stops any transfer in progress.
  1762.        ;; The next operation will open a new ftp connection.
  1763.        (delete-process proc)
  1764.        (signal 'quit nil)))))
  1765.  
  1766. (defun ange-ftp-nslookup-host (host)
  1767.   "Attempt to resolve the given HOSTNAME using nslookup if possible."
  1768.   (interactive "sHost:  ")
  1769.   (if ange-ftp-nslookup-program
  1770.       (let ((default-directory
  1771.           (if (file-accessible-directory-p default-directory)
  1772.           default-directory
  1773.         exec-directory))
  1774.         ;; It would be nice to make process-connection-type nil,
  1775.         ;; but that doesn't work: ftp never responds.
  1776.         ;; Can anyone find a fix for that?
  1777.         (proc (let ((process-connection-type t))
  1778.             (start-process " *nslookup*" " *nslookup*"
  1779.                    ange-ftp-nslookup-program host)))
  1780.         (res host))
  1781.     (process-kill-without-query proc)
  1782.     (save-excursion
  1783.       (set-buffer (process-buffer proc))
  1784.       (while (memq (process-status proc) '(run open))
  1785.         (accept-process-output proc))
  1786.       (goto-char (point-min))
  1787.       (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t)
  1788.           (setq res (buffer-substring (match-beginning 1)
  1789.                       (match-end 1))))
  1790.       (kill-buffer (current-buffer)))
  1791.     res)
  1792.     host))
  1793.  
  1794. (defun ange-ftp-start-process (host user name)
  1795.   "Spawn a new ftp process ready to connect to machine HOST and give it NAME.
  1796. If HOST is only ftp-able through a gateway machine then spawn a shell
  1797. on the gateway machine to do the ftp instead."
  1798.   (let* ((use-gateway (ange-ftp-use-gateway-p host))
  1799.      (use-smart-ftp (and (not ange-ftp-gateway-host)
  1800.                  (ange-ftp-use-smart-gateway-p host)))
  1801.      (ftp-prog (if (or use-gateway
  1802.                use-smart-ftp) 
  1803.                ange-ftp-gateway-ftp-program-name
  1804.              ange-ftp-ftp-program-name))
  1805.      (args (append (list ftp-prog) ange-ftp-ftp-program-args))
  1806.      ;; Without the following binding, ange-ftp-start-process
  1807.      ;; recurses on file-accessible-directory-p, since it needs to
  1808.      ;; restart its process in order to determine anything about
  1809.      ;; default-directory.
  1810.      (file-name-handler-alist)
  1811.      (default-directory
  1812.        (if (file-accessible-directory-p default-directory)
  1813.            default-directory
  1814.          exec-directory))
  1815.      proc)
  1816.     ;; It would be nice to make process-connection-type nil,
  1817.     ;; but that doesn't work: ftp never responds.
  1818.     ;; Can anyone find a fix for that?
  1819.     (let ((process-connection-type t)
  1820.       (process-environment process-environment))
  1821.       ;; This tells GNU ftp not to output any fancy escape sequences.
  1822.       (setenv "TERM" "dumb")
  1823.       (if use-gateway
  1824.       (if ange-ftp-gateway-program-interactive
  1825.           (setq proc (ange-ftp-gwp-start host user name args))
  1826.         (setq proc (apply 'start-process name name
  1827.                   (append (list ange-ftp-gateway-program
  1828.                         ange-ftp-gateway-host)
  1829.                       args))))
  1830.     (setq proc (apply 'start-process name name args))))
  1831.     (process-kill-without-query proc)
  1832.     (save-excursion
  1833.       (set-buffer (process-buffer proc))
  1834.       (internal-ange-ftp-mode))
  1835.     (set-process-sentinel proc (function ange-ftp-process-sentinel))
  1836.     (set-process-filter proc (function ange-ftp-process-filter))
  1837.     (accept-process-output proc)    ;wait for ftp startup message
  1838.     proc))
  1839.  
  1840. (defun internal-ange-ftp-mode ()
  1841.   "Major mode for interacting with the FTP process.
  1842.  
  1843. \\{comint-mode-map}"
  1844.   (interactive)
  1845.   (comint-mode)
  1846.   (setq major-mode 'internal-ange-ftp-mode)
  1847.   (setq mode-name "Internal Ange-ftp")
  1848.   (let ((proc (get-buffer-process (current-buffer))))
  1849.     (goto-char (point-max))
  1850.     (set-marker (process-mark proc) (point))
  1851.     (make-local-variable 'ange-ftp-process-string)
  1852.     (setq ange-ftp-process-string "")
  1853.     (make-local-variable 'ange-ftp-process-busy)
  1854.     (make-local-variable 'ange-ftp-process-result)
  1855.     (make-local-variable 'ange-ftp-process-msg)
  1856.     (make-local-variable 'ange-ftp-process-multi-skip)
  1857.     (make-local-variable 'ange-ftp-process-result-line)
  1858.     (make-local-variable 'ange-ftp-process-continue)
  1859.     (make-local-variable 'ange-ftp-hash-mark-count)
  1860.     (make-local-variable 'ange-ftp-binary-hash-mark-size)
  1861.     (make-local-variable 'ange-ftp-ascii-hash-mark-size)
  1862.     (make-local-variable 'ange-ftp-hash-mark-unit)
  1863.     (make-local-variable 'ange-ftp-xfer-size)
  1864.     (make-local-variable 'ange-ftp-last-percent)
  1865.     (setq ange-ftp-hash-mark-count 0)
  1866.     (setq ange-ftp-xfer-size 0)
  1867.     (setq ange-ftp-process-result-line "")
  1868.  
  1869.     (setq comint-prompt-regexp "^ftp> ")
  1870.     (make-local-variable 'comint-password-prompt-regexp)
  1871.     ;; This is a regexp that can't match anything.
  1872.     ;; ange-ftp has its own ways of handling passwords.
  1873.     (setq comint-password-prompt-regexp "^a\\'z")
  1874.     (make-local-variable 'paragraph-start)
  1875.     (setq paragraph-start comint-prompt-regexp)))
  1876.  
  1877. (defun ange-ftp-smart-login (host user pass account proc)
  1878.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1879. PROC is the FTP-client's process.  This routine uses the smart-gateway
  1880. host specified in ``ange-ftp-gateway-host''."
  1881.   (let ((result (ange-ftp-raw-send-cmd
  1882.          proc
  1883.          (format "open %s %s"
  1884.              (ange-ftp-nslookup-host ange-ftp-gateway-host)
  1885.              ange-ftp-smart-gateway-port)
  1886.          (format "Opening FTP connection to %s via %s"
  1887.              host
  1888.              ange-ftp-gateway-host))))
  1889.     (or (car result)
  1890.     (ange-ftp-error host user 
  1891.             (concat "OPEN request failed: "
  1892.                 (cdr result))))
  1893.     (setq result (ange-ftp-raw-send-cmd
  1894.           proc (format "user \"%s\"@%s %s %s"
  1895.                    user
  1896.                    (ange-ftp-nslookup-host host)
  1897.                    pass
  1898.                    account)
  1899.           (format "Logging in as user %s@%s"
  1900.               user host)))
  1901.     (or (car result)
  1902.     (progn
  1903.       (ange-ftp-set-passwd host user nil) ; reset password
  1904.       (ange-ftp-set-account host user nil) ; reset account
  1905.       (ange-ftp-error host user
  1906.               (concat "USER request failed: "
  1907.                   (cdr result)))))))
  1908.  
  1909. (defun ange-ftp-normal-login (host user pass account proc)
  1910.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1911. PROC is the process to the FTP-client."
  1912.   (let* ((nshost (ange-ftp-nslookup-host host))
  1913.      (result (ange-ftp-raw-send-cmd
  1914.          proc
  1915.          (format "open %s" nshost)
  1916.          (format "Opening FTP connection to %s" host))))
  1917.     (or (car result)
  1918.     (ange-ftp-error host user
  1919.             (concat "OPEN request failed: "
  1920.                 (cdr result))))
  1921.     (setq result (ange-ftp-raw-send-cmd
  1922.           proc
  1923.           (if (ange-ftp-use-smart-gateway-p host)
  1924.               (format "user \"%s\"@%s %s %s" user nshost pass account)
  1925.             (format "user \"%s\" %s %s" user pass account))
  1926.           (format "Logging in as user %s@%s" user host)))
  1927.     (or (car result)
  1928.     (progn
  1929.       (ange-ftp-set-passwd host user nil) ;reset password.
  1930.       (ange-ftp-set-account host user nil) ;reset account.
  1931.       (ange-ftp-error host user
  1932.               (concat "USER request failed: "
  1933.                   (cdr result)))))))
  1934.  
  1935. ;; ange@hplb.hpl.hp.com says this should not be changed.
  1936. (defvar ange-ftp-hash-mark-msgs
  1937.   "[hH]ash mark [^0-9]*\\([0-9]+\\)"
  1938.   "*Regexp matching the FTP client's output upon doing a HASH command.")
  1939.  
  1940. (defun ange-ftp-guess-hash-mark-size (proc)
  1941.   (if ange-ftp-send-hash
  1942.       (save-excursion
  1943.     (set-buffer (process-buffer proc))
  1944.     (let* ((status (ange-ftp-raw-send-cmd proc "hash"))
  1945.            (result (car status))
  1946.            (line (cdr status)))
  1947.       (save-match-data
  1948.         (if (string-match ange-ftp-hash-mark-msgs line)
  1949.         (let ((size (string-to-int
  1950.                 (substring line
  1951.                        (match-beginning 1)
  1952.                        (match-end 1)))))
  1953.           (setq ange-ftp-ascii-hash-mark-size size
  1954.             ange-ftp-hash-mark-unit (ash size -4))
  1955.  
  1956.           ;; if a default value for this is set, use that value.
  1957.           (or ange-ftp-binary-hash-mark-size
  1958.               (setq ange-ftp-binary-hash-mark-size size)))))))))
  1959.  
  1960. (defun ange-ftp-get-process (host user)
  1961.   "Return an FTP subprocess connected to HOST and logged in as USER.
  1962. Create a new process if needed."
  1963.   (let* ((name (ange-ftp-ftp-process-buffer host user))
  1964.      (proc (get-process name)))
  1965.     (if (and proc (memq (process-status proc) '(run open)))
  1966.     proc
  1967.       (let ((pass (ange-ftp-quote-string
  1968.            (ange-ftp-get-passwd host user)))
  1969.         (account (ange-ftp-quote-string
  1970.               (ange-ftp-get-account host user))))
  1971.     ;; grab a suitable process.
  1972.     (setq proc (ange-ftp-start-process host user name))
  1973.     
  1974.     ;; login to FTP server.
  1975.     (if (and (ange-ftp-use-smart-gateway-p host)
  1976.          ange-ftp-gateway-host)
  1977.         (ange-ftp-smart-login host user pass account proc)
  1978.       (ange-ftp-normal-login host user pass account proc))
  1979.       
  1980.     ;; Tell client to send back hash-marks as progress.  It isn't usually
  1981.     ;; fatal if this command fails.
  1982.     (ange-ftp-guess-hash-mark-size proc)
  1983.  
  1984.     ;; Guess at the host type.
  1985.     (ange-ftp-guess-host-type host user)
  1986.  
  1987.     ;; Run any user-specified hooks.  Note that proc, host and user are
  1988.     ;; dynamically bound at this point.
  1989.     (run-hooks 'ange-ftp-process-startup-hook))
  1990.       proc)))
  1991.  
  1992. ;; Variables for caching host and host-type
  1993. (defvar ange-ftp-host-cache nil)
  1994. (defvar ange-ftp-host-type-cache nil)
  1995.  
  1996. ;; If ange-ftp-host-type is called with the optional user
  1997. ;; argument, it will attempt to guess the host type by connecting
  1998. ;; as user, if necessary. For efficiency, I have tried to give this
  1999. ;; optional second argument only when necessary. Have I missed any calls
  2000. ;; to ange-ftp-host-type where it should have been supplied?
  2001.  
  2002. (defun ange-ftp-host-type (host &optional user)
  2003.   "Return a symbol which represents the type of the HOST given.
  2004. If the optional argument USER is given, attempts to guess the
  2005. host-type by logging in as USER."
  2006.   (if (eq host ange-ftp-host-cache)
  2007.       ange-ftp-host-type-cache
  2008.     ;; Trigger an ftp connection, in case we need to guess at the host type.
  2009.     (if (and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache))
  2010.     ange-ftp-host-type-cache
  2011.       (setq ange-ftp-host-cache host
  2012.         ange-ftp-host-type-cache
  2013.         (cond ((ange-ftp-dumb-unix-host host)
  2014.            'dumb-unix)
  2015. ;;          ((and (fboundp 'ange-ftp-vos-host)
  2016. ;;            (ange-ftp-vos-host host))
  2017. ;;           'vos)
  2018.           ((and (fboundp 'ange-ftp-vms-host)
  2019.             (ange-ftp-vms-host host))
  2020.            'vms)
  2021.           ((and (fboundp 'ange-ftp-mts-host)
  2022.             (ange-ftp-mts-host host))
  2023.            'mts)
  2024.           ((and (fboundp 'ange-ftp-cms-host)
  2025.             (ange-ftp-cms-host host))
  2026.            'cms)
  2027.           (t
  2028.            'unix))))))
  2029.  
  2030. ;; It would be nice to abstract the functions ange-ftp-TYPE-host and
  2031. ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions
  2032. ;; without sacrificing speed. Also, having separate variables
  2033. ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to
  2034. ;; set an alist to indicate that a host is of a given type. Even with
  2035. ;; automatic host type recognition, setting a regexp is still a good idea
  2036. ;; (for efficiency) if you log into a particular non-UNIX host frequently.
  2037.  
  2038. (defvar ange-ftp-fix-name-func-alist nil
  2039.   "Alist saying how to convert file name to the host's syntax.
  2040. Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2041. which can change a UNIX file name into a name more suitable for a host of type
  2042. TYPE.")
  2043.  
  2044. (defvar ange-ftp-fix-dir-name-func-alist nil
  2045.   "Alist saying how to convert directory name to the host's syntax.
  2046. Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2047. which can change UNIX directory name into a directory name more suitable
  2048. for a host of type TYPE.")
  2049.  
  2050. ;; *** Perhaps the sense of this variable should be inverted, since there
  2051. ;; *** is only 1 host type that can take ls-style listing options.
  2052. (defvar ange-ftp-dumb-host-types '(dumb-unix)
  2053.   "List of host types that can't take UNIX ls-style listing options.")
  2054.  
  2055. (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait)
  2056.   "Find an ftp process connected to HOST logged in as USER and send it CMD.
  2057. MSG is an optional status message to be output before and after issuing the
  2058. command.
  2059. See the documentation for ange-ftp-raw-send-cmd for a description of CONT
  2060. and NOWAIT."
  2061.   ;; Handle conversion to remote file name syntax and remote ls option
  2062.   ;; capability.
  2063.   (let ((cmd0 (car cmd))
  2064.     (cmd1 (nth 1 cmd))
  2065.     (ange-ftp-this-user user)
  2066.     (ange-ftp-this-host host)
  2067.     (ange-ftp-this-msg msg)
  2068.     cmd2 cmd3 host-type fix-name-func)
  2069.  
  2070.     (cond
  2071.      
  2072.      ;; pwd case (We don't care what host-type.)
  2073.      ((null cmd1))
  2074.      
  2075.      ;; cmd == 'dir "remote-name" "local-name" "ls-switches"
  2076.      ((progn
  2077.        (setq cmd2 (nth 2 cmd)
  2078.          host-type (ange-ftp-host-type host user))
  2079.        ;; This will trigger an FTP login, if one doesn't exist
  2080.        (eq cmd0 'dir))
  2081.       (setq cmd1 (funcall
  2082.           (or (cdr (assq host-type ange-ftp-fix-dir-name-func-alist))
  2083.               'identity)
  2084.           cmd1)
  2085.         cmd3 (nth 3 cmd))
  2086.       ;; Need to deal with the HP-UX ftp bug. This should also allow
  2087.       ;; us to resolve symlinks to directories on SysV machines. (Sebastian will
  2088.       ;; be happy.)
  2089.       (and (eq host-type 'unix)
  2090.        (string-match "/$" cmd1)
  2091.        (not (string-match "R" cmd3))
  2092.        (setq cmd1 (concat cmd1 ".")))
  2093.       ;; If the remote ls can take switches, put them in
  2094.       (or (memq host-type ange-ftp-dumb-host-types)
  2095.       (setq cmd0 'ls
  2096.         cmd1 (format "\"%s %s\"" cmd3 cmd1))))
  2097.      
  2098.      ;; First argument is the remote name
  2099.      ((progn
  2100.     (setq fix-name-func (or (cdr (assq host-type
  2101.                        ange-ftp-fix-name-func-alist))
  2102.                 'identity))
  2103.     (memq cmd0 '(get delete mkdir rmdir cd)))
  2104.       (setq cmd1 (funcall fix-name-func cmd1)))
  2105.  
  2106.      ;; Second argument is the remote name
  2107.      ((memq cmd0 '(append put chmod))
  2108.       (setq cmd2 (funcall fix-name-func cmd2)))
  2109.  
  2110.      ;; Both arguments are remote names
  2111.      ((eq cmd0 'rename)
  2112.       (setq cmd1 (funcall fix-name-func cmd1)
  2113.         cmd2 (funcall fix-name-func cmd2))))
  2114.       
  2115.     ;; Turn the command into one long string 
  2116.     (setq cmd0 (symbol-name cmd0))
  2117.     (setq cmd (concat cmd0
  2118.               (and cmd1 (concat " " cmd1))
  2119.               (and cmd2 (concat " " cmd2))))
  2120.  
  2121.     ;; Actually send the resulting command.
  2122.     (let (afsc-result
  2123.       afsc-line)
  2124.       (ange-ftp-raw-send-cmd
  2125.        (ange-ftp-get-process host user)
  2126.        cmd
  2127.        msg
  2128.        (list
  2129.     (function (lambda (result line host user
  2130.                   cmd msg cont nowait)
  2131.             (or cont
  2132.             (setq afsc-result result
  2133.                   afsc-line line))
  2134.             (if result
  2135.             (ange-ftp-call-cont cont result line)
  2136.               (ange-ftp-raw-send-cmd
  2137.                (ange-ftp-get-process host user)
  2138.                cmd
  2139.                msg
  2140.                (list
  2141.             (function (lambda (result line cont)
  2142.                     (or cont
  2143.                     (setq afsc-result result
  2144.                           afsc-line line))
  2145.                     (ange-ftp-call-cont cont result line)))
  2146.             cont)
  2147.                nowait))))
  2148.     host user cmd msg cont nowait)
  2149.        nowait)
  2150.  
  2151.       (if nowait
  2152.       nil
  2153.     (if cont
  2154.         nil
  2155.       (cons afsc-result afsc-line))))))
  2156.  
  2157. ;; It might be nice to message users about the host type identified,
  2158. ;; but there is so much other messaging going on, it would not be
  2159. ;; seen. No point in slowing things down just so users can read
  2160. ;; a host type message.
  2161.  
  2162. (defconst ange-ftp-cms-name-template
  2163.   (concat
  2164.    "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
  2165.    "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
  2166. (defconst ange-ftp-vms-name-template
  2167.   "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
  2168. (defconst ange-ftp-mts-name-template
  2169.   "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
  2170.  
  2171. (defun ange-ftp-guess-host-type (host user)
  2172.   "Guess at the the host type of HOST.
  2173. Works by doing a pwd and examining the directory syntax."
  2174.   (let ((host-type (ange-ftp-host-type host))
  2175.     (key (concat host "/" user "/~")))
  2176.     (if (eq host-type 'unix)
  2177.     ;; Note that ange-ftp-host-type returns unix as the default value.
  2178.     (save-match-data
  2179.       (let* ((result (ange-ftp-get-pwd host user))
  2180.          (dir (car result))
  2181.          fix-name-func)
  2182.         (cond ((null dir)
  2183.            (message "Warning! Unable to get home directory")
  2184.            (sit-for 1)
  2185.            (if (string-match
  2186.             "^450 No current working directory defined$"
  2187.             (cdr result))
  2188.                
  2189.                ;; We'll assume that if pwd bombs with this
  2190.                ;; error message, then it's CMS.
  2191.                (progn
  2192.              (ange-ftp-add-cms-host host)
  2193.              (setq ange-ftp-host-cache host
  2194.                    ange-ftp-host-type-cache 'cms))))
  2195.  
  2196.           ;; try for VMS
  2197.           ((string-match ange-ftp-vms-name-template dir)
  2198.            (ange-ftp-add-vms-host host)
  2199.            ;; The add-host functions clear the host type cache.
  2200.            ;; Therefore, need to set the cache afterwards.
  2201.            (setq ange-ftp-host-cache host
  2202.              ange-ftp-host-type-cache 'vms))
  2203.  
  2204.           ;; try for MTS
  2205.           ((string-match ange-ftp-mts-name-template dir)
  2206.            (ange-ftp-add-mts-host host)
  2207.            (setq ange-ftp-host-cache host
  2208.              ange-ftp-host-type-cache 'mts))
  2209.           
  2210.           ;; try for CMS
  2211.           ((string-match ange-ftp-cms-name-template dir)
  2212.            (ange-ftp-add-cms-host host)
  2213.            (setq ange-ftp-host-cache host
  2214.              ange-ftp-host-type-cache 'cms))
  2215.  
  2216.           ;; assume UN*X
  2217.           (t
  2218.            (setq ange-ftp-host-cache host
  2219.              ange-ftp-host-type-cache 'unix)))
  2220.  
  2221.         ;; Now that we have done a pwd, might as well put it in
  2222.         ;; the expand-dir hashtable.
  2223.         (let ((ange-ftp-this-user user)
  2224.           (ange-ftp-this-host host))
  2225.           (setq fix-name-func (cdr (assq ange-ftp-host-type-cache
  2226.                          ange-ftp-fix-name-func-alist)))
  2227.           (if fix-name-func
  2228.           (setq dir (funcall fix-name-func dir 'reverse))))
  2229.         (ange-ftp-put-hash-entry key dir
  2230.                      ange-ftp-expand-dir-hashtable))))
  2231.  
  2232.     ;; In the special case of CMS make sure that know the
  2233.     ;; expansion of the home minidisk now, because we will
  2234.     ;; be doing a lot of cd's.
  2235.     (if (and (eq host-type 'cms)
  2236.          (not (ange-ftp-hash-entry-exists-p
  2237.            key ange-ftp-expand-dir-hashtable)))
  2238.     (let ((dir (car (ange-ftp-get-pwd host user))))
  2239.       (if dir
  2240.           (ange-ftp-put-hash-entry key (concat "/" dir)
  2241.                        ange-ftp-expand-dir-hashtable)
  2242.         (message "Warning! Unable to get home directory")
  2243.         (sit-for 1))))))
  2244.  
  2245.  
  2246. ;;;; ------------------------------------------------------------
  2247. ;;;; Remote file and directory listing support.
  2248. ;;;; ------------------------------------------------------------
  2249.  
  2250. ;; Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
  2251. ;; to take switch arguments.
  2252. (defun ange-ftp-dumb-unix-host (host)
  2253.   (and host ange-ftp-dumb-unix-host-regexp
  2254.        (save-match-data
  2255.      (string-match ange-ftp-dumb-unix-host-regexp host))))
  2256.  
  2257. (defun ange-ftp-add-dumb-unix-host (host)
  2258.   "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
  2259.   (interactive
  2260.    (list (read-string "Host: "
  2261.               (let ((name (or (buffer-file-name) default-directory)))
  2262.             (and name (car (ange-ftp-ftp-name name)))))))
  2263.   (if (not (ange-ftp-dumb-unix-host host))
  2264.       (setq ange-ftp-dumb-unix-host-regexp
  2265.         (concat "^" (regexp-quote host) "$"
  2266.             (and ange-ftp-dumb-unix-host-regexp "\\|")
  2267.             ange-ftp-dumb-unix-host-regexp)
  2268.         ange-ftp-host-cache nil)))
  2269.  
  2270. (defvar ange-ftp-parse-list-func-alist nil
  2271.   "Alist saying how to parse directory listings for certain OS types.
  2272. Association list of \( TYPE \. FUNC \) pairs.  The FUNC is a routine
  2273. which can parse the output from a DIR listing for a host of type TYPE.")
  2274.  
  2275. ;; With no-error nil, this function returns:
  2276. ;; an error if file is not an ange-ftp-name
  2277. ;;                      (This should never happen.) 
  2278. ;; an error if either the listing is unreadable or there is an ftp error.
  2279. ;; the listing (a string), if everything works.
  2280. ;; 
  2281. ;; With no-error t, it returns:
  2282. ;; an error if not an ange-ftp-name
  2283. ;; error if listing is unreadable (most likely caused by a slow connection)
  2284. ;; nil if ftp error (this is because although asking to list a nonexistent
  2285. ;;                   directory on a remote unix machine usually (except
  2286. ;;                   maybe for dumb hosts) returns an ls error, but no
  2287. ;;                   ftp error, if the same is done on a VMS machine,
  2288. ;;                   an ftp error is returned. Need to trap the error
  2289. ;;                   so we can go on and try to list the parent.)
  2290. ;; the listing, if everything works.
  2291.  
  2292. ;; If WILDCARD is non-nil, then this implements the guts of insert-directory
  2293. ;; in the wildcard case.  Then we make a relative directory listing
  2294. ;; of FILE within the directory specified by `default-directory'.
  2295.  
  2296. (defun ange-ftp-ls (file lsargs parse &optional no-error wildcard)
  2297.   "Return the output of an `DIR' or `ls' command done over ftp.
  2298. FILE is the full name of the remote file, LSARGS is any args to pass to the
  2299. `ls' command, and PARSE specifies that the output should be parsed and stored
  2300. away in the internal cache."
  2301.   ;; If parse is t, we assume that file is a directory. i.e. we only parse
  2302.   ;; full directory listings.
  2303.   (let* ((ange-ftp-this-file (ange-ftp-expand-file-name file))
  2304.      (parsed (ange-ftp-ftp-name ange-ftp-this-file)))
  2305.     (if parsed
  2306.     (let* ((host (nth 0 parsed))
  2307.            (user (nth 1 parsed))
  2308.            (name (ange-ftp-quote-string (nth 2 parsed)))
  2309.            (key (directory-file-name ange-ftp-this-file))
  2310.            (host-type (ange-ftp-host-type host user))
  2311.            (dumb (memq host-type ange-ftp-dumb-host-types))
  2312.            result
  2313.            temp
  2314.            lscmd parse-func)
  2315.       (if (string-equal name "")
  2316.           (setq name
  2317.             (ange-ftp-real-file-name-as-directory
  2318.                   (ange-ftp-expand-dir host user "~"))))
  2319.       (if (and ange-ftp-ls-cache-file
  2320.            (string-equal key ange-ftp-ls-cache-file)
  2321.            ;; Don't care about lsargs for dumb hosts.
  2322.            (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
  2323.           ange-ftp-ls-cache-res
  2324.         (setq temp (ange-ftp-make-tmp-name host))
  2325.         (if wildcard
  2326.         (progn
  2327.           (ange-ftp-cd host user (file-name-directory name))
  2328.           (setq lscmd (list 'dir file temp lsargs)))
  2329.           (setq lscmd (list 'dir name temp lsargs)))
  2330.         (unwind-protect
  2331.         (if (car (setq result (ange-ftp-send-cmd
  2332.                        host
  2333.                        user
  2334.                        lscmd
  2335.                        (format "Listing %s"
  2336.                            (ange-ftp-abbreviate-filename
  2337.                         ange-ftp-this-file)))))
  2338.             (save-excursion
  2339.               (set-buffer (get-buffer-create
  2340.                    ange-ftp-data-buffer-name))
  2341.               (erase-buffer)
  2342.               (if (ange-ftp-real-file-readable-p temp)
  2343.               (ange-ftp-real-insert-file-contents temp)
  2344.             (sleep-for ange-ftp-retry-time)
  2345.                     ;wait for file to possibly appear
  2346.             (if (ange-ftp-real-file-readable-p temp)
  2347.                 ;; Try again.
  2348.                 (ange-ftp-real-insert-file-contents temp)
  2349.               (ange-ftp-error host user
  2350.                       (format
  2351.                        "list data file %s not readable"
  2352.                        temp))))
  2353.               (if parse
  2354.               (ange-ftp-set-files
  2355.                ange-ftp-this-file
  2356.                (if (setq
  2357.                 parse-func
  2358.                 (cdr (assq host-type
  2359.                        ange-ftp-parse-list-func-alist)))
  2360.                    (funcall parse-func)
  2361.                  (ange-ftp-parse-dired-listing lsargs))))
  2362.               (setq ange-ftp-ls-cache-file key
  2363.                 ange-ftp-ls-cache-lsargs lsargs
  2364.                     ; For dumb hosts-types this is
  2365.                     ; meaningless but harmless.
  2366.                 ange-ftp-ls-cache-res (buffer-string))
  2367.               ;; (kill-buffer (current-buffer))
  2368.               ange-ftp-ls-cache-res)
  2369.           (if no-error
  2370.               nil
  2371.             (ange-ftp-error host user
  2372.                     (concat "DIR failed: " (cdr result)))))
  2373.           (ange-ftp-del-tmp-name temp))))
  2374.       (error "Should never happen. Please report. Bug ref. no.: 1"))))
  2375.  
  2376. ;;;; ------------------------------------------------------------
  2377. ;;;; Directory information caching support.
  2378. ;;;; ------------------------------------------------------------
  2379.  
  2380. (defconst ange-ftp-date-regexp
  2381.   (concat
  2382.    " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
  2383.    "\\|Nov\\|Dec\\) +[0-3]?[0-9] "))
  2384.  
  2385. (defvar ange-ftp-add-file-entry-alist nil
  2386.   "Alist saying how to add file entries on certain OS types.
  2387. Association list of pairs \( TYPE \. FUNC \), where FUNC
  2388. is a function to be used to add a file entry for the OS TYPE. The
  2389. main reason for this alist is to deal with file versions in VMS.")
  2390.  
  2391. (defvar ange-ftp-delete-file-entry-alist nil
  2392.   "Alist saying how to delete files on certain OS types.
  2393. Association list of pairs \( TYPE \. FUNC \), where FUNC
  2394. is a function to be used to delete a file entry for the OS TYPE.
  2395. The main reason for this alist is to deal with file versions in VMS.")
  2396.  
  2397. (defun ange-ftp-add-file-entry (name &optional dir-p)
  2398.   "Add a file entry for file NAME, if its directory info exists."
  2399.   (funcall (or (cdr (assq (ange-ftp-host-type
  2400.                (car (ange-ftp-ftp-name name)))
  2401.               ange-ftp-add-file-entry-alist))
  2402.            'ange-ftp-internal-add-file-entry)
  2403.        name dir-p)
  2404.   (setq ange-ftp-ls-cache-file nil))
  2405.  
  2406. (defun ange-ftp-delete-file-entry (name &optional dir-p)
  2407.   "Delete the file entry for file NAME, if its directory info exists."
  2408.   (funcall (or (cdr (assq (ange-ftp-host-type
  2409.                (car (ange-ftp-ftp-name name)))
  2410.               ange-ftp-delete-file-entry-alist))
  2411.            'ange-ftp-internal-delete-file-entry)
  2412.        name dir-p)
  2413.   (setq ange-ftp-ls-cache-file nil))
  2414.  
  2415. (defmacro ange-ftp-parse-filename ()
  2416.   ;;Extract the filename from the current line of a dired-like listing.
  2417.   (` (let ((eol (progn (end-of-line) (point))))
  2418.        (beginning-of-line)
  2419.        (if (re-search-forward ange-ftp-date-regexp eol t)
  2420.        (progn
  2421.          (skip-chars-forward " ")
  2422.          (skip-chars-forward "^ " eol)
  2423.          (skip-chars-forward " " eol)
  2424.          ;; We bomb on filenames starting with a space.
  2425.          (buffer-substring (point) eol))))))
  2426.   
  2427. ;; This deals with the F switch. Should also do something about
  2428. ;; unquoting names obtained with the SysV b switch and the GNU Q
  2429. ;; switch. See Sebastian's dired-get-filename.
  2430.  
  2431. (defmacro ange-ftp-ls-parser ()
  2432.   ;; Note that switches is dynamically bound.
  2433.   ;; Meant to be called by ange-ftp-parse-dired-listing
  2434.   (` (let ((tbl (ange-ftp-make-hashtable))
  2435.        (used-F (and (stringp switches)
  2436.             (string-match "F" switches)))
  2437.        file-type symlink directory file)
  2438.        (while (setq file (ange-ftp-parse-filename))
  2439.      (beginning-of-line)
  2440.      (skip-chars-forward "\t 0-9")
  2441.      (setq file-type (following-char)
  2442.            directory (eq file-type ?d))
  2443.      (if (eq file-type ?l)
  2444.          (if (string-match " -> " file)
  2445.          (setq symlink (substring file (match-end 0))
  2446.                file (substring file 0 (match-beginning 0)))
  2447.            ;; Shouldn't happen
  2448.            (setq symlink ""))
  2449.        (setq symlink nil))
  2450.      ;; Only do a costly regexp search if the F switch was used.
  2451.      (if (and used-F
  2452.           (not (string-equal file ""))
  2453.           (looking-at
  2454.            ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
  2455.          (let ((socket (eq file-type ?s))
  2456.            (executable
  2457.             (and (not symlink) ; x bits don't mean a thing for symlinks
  2458.              (string-match "[xst]"
  2459.                        (concat
  2460.                     (buffer-substring
  2461.                      (match-beginning 1)
  2462.                      (match-end 1))
  2463.                     (buffer-substring
  2464.                      (match-beginning 2)
  2465.                      (match-end 2))
  2466.                     (buffer-substring
  2467.                      (match-beginning 3)
  2468.                      (match-end 3)))))))
  2469.            ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
  2470.            ;; and others don't. (sigh...) Beware, that some Unix's don't
  2471.            ;; seem to believe in the F-switch
  2472.            (if (or (and symlink (string-match "@$" file))
  2473.                (and directory (string-match "/$" file))
  2474.                (and executable (string-match "*$" file))
  2475.                (and socket (string-match "=$" file)))
  2476.            (setq file (substring file 0 -1)))))
  2477.      (ange-ftp-put-hash-entry file (or symlink directory) tbl)
  2478.      (forward-line 1))
  2479.        (ange-ftp-put-hash-entry "." t tbl)
  2480.        (ange-ftp-put-hash-entry ".." t tbl)
  2481.        tbl)))
  2482.  
  2483. ;;; The dl stuff for descriptive listings
  2484.  
  2485. (defvar ange-ftp-dl-dir-regexp nil
  2486.   "Regexp matching directories which are listed in dl format.
  2487. This regexp should not be anchored with a trailing `$', because it should
  2488. match subdirectories as well.")
  2489.  
  2490. (defun ange-ftp-add-dl-dir (dir)
  2491.   "Interactively adds a DIR to ange-ftp-dl-dir-regexp."
  2492.   (interactive
  2493.    (list (read-string "Directory: "
  2494.               (let ((name (or (buffer-file-name) default-directory)))
  2495.             (and name (ange-ftp-ftp-name name)
  2496.                  (file-name-directory name))))))
  2497.   (if (not (and ange-ftp-dl-dir-regexp
  2498.         (string-match ange-ftp-dl-dir-regexp dir)))
  2499.       (setq ange-ftp-dl-dir-regexp
  2500.         (concat "^" (regexp-quote dir)
  2501.             (and ange-ftp-dl-dir-regexp "\\|")
  2502.             ange-ftp-dl-dir-regexp))))
  2503.  
  2504. (defmacro ange-ftp-dl-parser ()
  2505.   ;; Parse the current buffer, which is assumed to be a descriptive
  2506.   ;; listing, and return a hashtable.
  2507.   (` (let ((tbl (ange-ftp-make-hashtable)))
  2508.        (while (not (eobp))
  2509.      (ange-ftp-put-hash-entry
  2510.       (buffer-substring (point)
  2511.                 (progn
  2512.                   (skip-chars-forward "^ /\n")
  2513.                   (point)))
  2514.       (eq (following-char) ?/)
  2515.       tbl)
  2516.      (forward-line 1))
  2517.        (ange-ftp-put-hash-entry "." t tbl)
  2518.        (ange-ftp-put-hash-entry ".." t tbl)
  2519.        tbl)))
  2520.  
  2521. ;; Parse the current buffer which is assumed to be in a dired-like listing
  2522. ;; format, and return a hashtable as the result. If the listing is not really
  2523. ;; a listing, then return nil.
  2524.  
  2525. (defun ange-ftp-parse-dired-listing (&optional switches)
  2526.   (save-match-data
  2527.     (cond
  2528.      ((looking-at "^total [0-9]+$")
  2529.       (forward-line 1)
  2530.       ;; Some systems put in a blank line here.
  2531.       (if (eolp) (forward-line 1))
  2532.       (ange-ftp-ls-parser))
  2533.      ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
  2534.       ;; It's an ls error message.
  2535.       nil)
  2536.      ((eobp) ; i.e. (zerop (buffer-size))
  2537.       ;; This could be one of:
  2538.       ;; (1) An Ultrix ls error message
  2539.       ;; (2) A listing with the A switch of an empty directory
  2540.       ;;     on a machine which doesn't give a total line.
  2541.       ;; (3) The twilight zone.
  2542.       ;; We'll assume (1) for now.
  2543.       nil)
  2544.      ((re-search-forward ange-ftp-date-regexp nil t)
  2545.       (beginning-of-line)
  2546.       (ange-ftp-ls-parser))
  2547.      ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
  2548.       ;; It's a dl listing (I hope).
  2549.       ;; file is bound by the call to ange-ftp-ls
  2550.       (ange-ftp-add-dl-dir ange-ftp-this-file)
  2551.       (beginning-of-line)
  2552.       (ange-ftp-dl-parser))
  2553.      (t nil))))
  2554.  
  2555. (defun ange-ftp-set-files (directory files)
  2556.   "For a given DIRECTORY, set or change the associated FILES hashtable."
  2557.   (and files (ange-ftp-put-hash-entry (file-name-as-directory directory)
  2558.                       files ange-ftp-files-hashtable)))
  2559.  
  2560. (defun ange-ftp-get-files (directory &optional no-error)
  2561.   "Given a given DIRECTORY, return a hashtable of file entries.
  2562. This will give an error or return nil, depending on the value of
  2563. NO-ERROR, if a listing for DIRECTORY cannot be obtained."
  2564.   (setq directory (file-name-as-directory directory)) ;normalize
  2565.   (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable)
  2566.       (save-match-data
  2567.     (and (ange-ftp-ls directory
  2568.               ;; This is an efficiency hack. We try to
  2569.               ;; anticipate what sort of listing dired
  2570.               ;; might want, and cache just such a listing.
  2571.               (if (and (boundp 'dired-actual-switches)
  2572.                    (stringp dired-actual-switches)
  2573.                    ;; We allow the A switch, which lists
  2574.                    ;; all files except "." and "..".
  2575.                    ;; This is OK because we manually
  2576.                    ;; insert these entries
  2577.                    ;; in the hash table.
  2578.                    (string-match
  2579.                     "[aA]" dired-actual-switches)
  2580.                    (string-match
  2581.                     "l" dired-actual-switches)
  2582.                    (not (string-match
  2583.                      "R" dired-actual-switches)))
  2584.                   dired-actual-switches
  2585.                 (if (and (boundp 'dired-listing-switches)
  2586.                      (stringp dired-listing-switches)
  2587.                      (string-match
  2588.                       "[aA]" dired-listing-switches)
  2589.                      (string-match
  2590.                       "l" dired-listing-switches)
  2591.                      (not (string-match
  2592.                        "R" dired-listing-switches)))
  2593.                 dired-listing-switches
  2594.                   "-al"))
  2595.               t no-error)
  2596.          (ange-ftp-get-hash-entry
  2597.           directory ange-ftp-files-hashtable)))))
  2598.  
  2599. ;; Given NAME, return the file part that can be used for looking up the
  2600. ;; file's entry in a hashtable.
  2601. (defmacro ange-ftp-get-file-part (name)
  2602.   (` (let ((file (file-name-nondirectory (, name))))
  2603.        (if (string-equal file "")
  2604.        "."
  2605.      file))))
  2606.  
  2607. ;; Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
  2608. ;; allowed to determine if NAME is a sub-directory by listing it directly,
  2609. ;; rather than listing its parent directory. This is used for efficiency so
  2610. ;; that a wasted listing is not done:
  2611. ;; 1. When looking for a .dired file in dired-x.el.
  2612. ;; 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
  2613. ;;     subdirectory. This is of course an OS dependent judgement.
  2614.  
  2615. (defmacro ange-ftp-allow-child-lookup (dir file)
  2616.   (` (not
  2617.       (let* ((efile (, file)) ; expand once.
  2618.          (edir (, dir))
  2619.          (parsed (ange-ftp-ftp-name edir))
  2620.          (host-type (ange-ftp-host-type
  2621.              (car parsed))))
  2622.     (or
  2623. ;;; This variable seems not to exist in Emacs 19 -- rms.
  2624. ;;;     ;; Deal with dired
  2625. ;;;     (and (boundp 'dired-local-variables-file)
  2626. ;;;          (stringp dired-local-variables-file)
  2627. ;;;          (string-equal dired-local-variables-file efile))
  2628.      ;; No dots in dir names in vms.
  2629.      (and (eq host-type 'vms)
  2630.           (string-match "\\." efile))
  2631.      ;; No subdirs in mts of cms.
  2632.      (and (memq host-type '(mts cms))
  2633.           (not (string-equal "/" (nth 2 parsed)))))))))
  2634.  
  2635. (defun ange-ftp-file-entry-p (name)
  2636.   "Given NAME, return whether there is a file entry for it."
  2637.   (let* ((name (directory-file-name name))
  2638.      (dir (file-name-directory name))
  2639.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2640.      (file (ange-ftp-get-file-part name)))
  2641.     (if ent
  2642.     (ange-ftp-hash-entry-exists-p file ent)
  2643.       (or (and (ange-ftp-allow-child-lookup dir file)
  2644.            (setq ent (ange-ftp-get-files name t))
  2645.            ;; Try a child lookup. i.e. try to list file as a
  2646.            ;; subdirectory of dir. This is a good idea because
  2647.            ;; we may not have read permission for file's parent. Also,
  2648.            ;; people tend to work down directory trees anyway. We use
  2649.            ;; no-error ;; because if file does not exist as a subdir.,
  2650.            ;; then dumb hosts will give an ftp error. Smart unix hosts
  2651.            ;; will simply send back the ls
  2652.            ;; error message.
  2653.            (ange-ftp-get-hash-entry "." ent))
  2654.       ;; Child lookup failed. Try the parent. If this bombs,
  2655.       ;; we are at wits end -- signal an error.
  2656.       ;; Problem: If this signals an error, the error message
  2657.       ;; may  not have a lot to do with what went wrong.
  2658.       (ange-ftp-hash-entry-exists-p file
  2659.                     (ange-ftp-get-files dir))))))
  2660.  
  2661. (defun ange-ftp-get-file-entry (name)
  2662.   "Given NAME, return the given file entry.
  2663. The entry will be either t for a directory, nil for a normal file,
  2664. or a string for a symlink. If the file isn't in the hashtable,
  2665. this also returns nil."
  2666.   (let* ((name (directory-file-name name))
  2667.      (dir (file-name-directory name))
  2668.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2669.      (file (ange-ftp-get-file-part name)))
  2670.     (if ent
  2671.     (ange-ftp-get-hash-entry file ent)
  2672.       (or (and (ange-ftp-allow-child-lookup dir file)
  2673.            (setq ent (ange-ftp-get-files name t))
  2674.            (ange-ftp-get-hash-entry "." ent))
  2675.            ;; i.e. it's a directory by child lookup
  2676.       (ange-ftp-get-hash-entry file
  2677.                    (ange-ftp-get-files dir))))))
  2678.  
  2679. (defun ange-ftp-internal-delete-file-entry (name &optional dir-p)
  2680.   (if dir-p
  2681.       (progn 
  2682.     (setq name (file-name-as-directory name))
  2683.     (ange-ftp-del-hash-entry name ange-ftp-files-hashtable)
  2684.     (setq name (directory-file-name name))))
  2685.   ;; Note that file-name-as-directory followed by directory-file-name
  2686.   ;; serves to canonicalize directory file names to their unix form.
  2687.   ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
  2688.   (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
  2689.                     ange-ftp-files-hashtable)))
  2690.     (if files
  2691.     (ange-ftp-del-hash-entry (ange-ftp-get-file-part name)
  2692.                  files))))
  2693.  
  2694. (defun ange-ftp-internal-add-file-entry (name &optional dir-p)
  2695.   (and dir-p
  2696.        (setq name (directory-file-name name)))
  2697.   (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
  2698.                     ange-ftp-files-hashtable)))
  2699.     (if files
  2700.     (ange-ftp-put-hash-entry (ange-ftp-get-file-part name)
  2701.                  dir-p
  2702.                  files))))
  2703.  
  2704. (defun ange-ftp-wipe-file-entries (host user)
  2705.   "Get rid of entry for HOST, USER pair from file entry information hashtable."
  2706.   (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable))))
  2707.     (ange-ftp-map-hashtable
  2708.      (function
  2709.       (lambda (key val)
  2710.     (let ((parsed (ange-ftp-ftp-name key)))
  2711.       (if parsed
  2712.           (let ((h (nth 0 parsed))
  2713.             (u (nth 1 parsed)))
  2714.         (or (and (equal host h) (equal user u))
  2715.             (ange-ftp-put-hash-entry key val new-tbl)))))))
  2716.      ange-ftp-files-hashtable)
  2717.     (setq ange-ftp-files-hashtable new-tbl)))
  2718.  
  2719. ;;;; ------------------------------------------------------------
  2720. ;;;; File transfer mode support.
  2721. ;;;; ------------------------------------------------------------
  2722.  
  2723. (defun ange-ftp-set-binary-mode (host user)
  2724.   "Tell the ftp process for the given HOST & USER to switch to binary mode."
  2725.   (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
  2726.     (if (not (car result))
  2727.     (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
  2728.       (save-excursion
  2729.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2730.     (and ange-ftp-binary-hash-mark-size
  2731.          (setq ange-ftp-hash-mark-unit
  2732.            (ash ange-ftp-binary-hash-mark-size -4)))))))
  2733.  
  2734. (defun ange-ftp-set-ascii-mode (host user)
  2735.   "Tell the ftp process for the given HOST & USER to switch to ascii mode."
  2736.   (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
  2737.     (if (not (car result))
  2738.     (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
  2739.       (save-excursion
  2740.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2741.     (and ange-ftp-ascii-hash-mark-size
  2742.          (setq ange-ftp-hash-mark-unit
  2743.            (ash ange-ftp-ascii-hash-mark-size -4)))))))
  2744.  
  2745. (defun ange-ftp-cd (host user dir)
  2746.   (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
  2747.     (or (car result)
  2748.     (ange-ftp-error host user (concat "CD failed: " (cdr result))))))
  2749.  
  2750. (defun ange-ftp-get-pwd (host user)
  2751.   "Attempts to get the current working directory for the given HOST/USER pair.
  2752. Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
  2753. and LINE is the relevant success or fail line from the FTP-client."
  2754.   (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
  2755.      (line (cdr result))
  2756.      dir)
  2757.     (if (car result)
  2758.     (save-match-data
  2759.       (and (or (string-match "\"\\([^\"]*\\)\"" line)
  2760.            (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
  2761.            (setq dir (substring line
  2762.                     (match-beginning 1)
  2763.                     (match-end 1))))))
  2764.     (cons dir line)))
  2765.  
  2766. ;;; ------------------------------------------------------------
  2767. ;;; expand-file-name and friends...which currently don't work
  2768. ;;; ------------------------------------------------------------
  2769.  
  2770. (defun ange-ftp-expand-dir (host user dir)
  2771.   "Return the result of doing a PWD in the current FTP session.
  2772. Use the connection to machine HOST
  2773. logged in as user USER and cd'd to directory DIR."
  2774.   (let* ((host-type (ange-ftp-host-type host user))
  2775.      ;; It is more efficient to call ange-ftp-host-type
  2776.      ;; before binding res, because ange-ftp-host-type sometimes
  2777.      ;; adds to the info in the expand-dir-hashtable.
  2778.      (fix-name-func
  2779.       (cdr (assq host-type ange-ftp-fix-name-func-alist)))
  2780.      (key (concat host "/" user "/" dir))
  2781.      (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable)))
  2782.     (or res
  2783.     (progn
  2784.       (or
  2785.        (string-equal user "anonymous")
  2786.        (string-equal user "ftp")
  2787.        (not (eq host-type 'unix))
  2788.        (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
  2789.                           "\\|"
  2790.                           ange-ftp-good-msgs))
  2791.           (result (ange-ftp-send-cmd host user
  2792.                          (list 'get dir "/dev/null")
  2793.                          (format "expanding %s" dir)))
  2794.           (line (cdr result)))
  2795.          (setq res
  2796.            (if (string-match ange-ftp-expand-dir-regexp line)
  2797.                (substring line 
  2798.                   (match-beginning 1)
  2799.                   (match-end 1))))))
  2800.       (or res
  2801.           (if (string-equal dir "~")
  2802.           (setq res (car (ange-ftp-get-pwd host user)))
  2803.         (let ((home (ange-ftp-expand-dir host user "~")))
  2804.           (unwind-protect
  2805.               (and (ange-ftp-cd host user dir)
  2806.                (setq res (car (ange-ftp-get-pwd host user))))
  2807.             (ange-ftp-cd host user home)))))
  2808.       (if res
  2809.           (let ((ange-ftp-this-user user)
  2810.             (ange-ftp-this-host host))
  2811.         (if fix-name-func
  2812.             (setq res (funcall fix-name-func res 'reverse)))
  2813.         (ange-ftp-put-hash-entry
  2814.          key res ange-ftp-expand-dir-hashtable)))
  2815.       res))))
  2816.  
  2817. (defun ange-ftp-canonize-filename (n)
  2818.   "Take a string and short-circuit //, /. and /.."
  2819.   (if (string-match "[^:]+//" n)        ;don't upset Apollo users
  2820.       (setq n (substring n (1- (match-end 0)))))
  2821.   (let ((parsed (ange-ftp-ftp-name n)))
  2822.     (if parsed
  2823.     (let ((host (car parsed))
  2824.           (user (nth 1 parsed))
  2825.           (name (nth 2 parsed)))
  2826.       
  2827.       ;; See if remote name is absolute.  If so then just expand it and
  2828.       ;; replace the name component of the overall name.
  2829.       (cond ((string-match "^/" name)
  2830.          name)
  2831.         
  2832.         ;; Name starts with ~ or ~user.  Resolve that part of the name
  2833.         ;; making it absolute then re-expand it.
  2834.         ((string-match "^~[^/]*" name)
  2835.          (let* ((tilda (substring name
  2836.                       (match-beginning 0) 
  2837.                       (match-end 0)))
  2838.             (rest (substring name (match-end 0)))
  2839.             (dir (ange-ftp-expand-dir host user tilda)))
  2840.            (if dir
  2841.                (setq name (concat dir rest))
  2842.              (error "User \"%s\" is not known" 
  2843.                 (substring tilda 1)))))
  2844.         
  2845.         ;; relative name.  Tack on homedir and re-expand.
  2846.         (t
  2847.          (let ((dir (ange-ftp-expand-dir host user "~")))
  2848.            (if dir
  2849.                (setq name (concat
  2850.                    (ange-ftp-real-file-name-as-directory dir)
  2851.                    name))
  2852.              (error "Unable to obtain CWD")))))
  2853.       
  2854.       ;; If name starts with //, preserve that, for apollo system.
  2855.       (if (not (string-match "^//" name))
  2856.           (progn
  2857.         (setq name (ange-ftp-real-expand-file-name name))
  2858.  
  2859.         (if (string-match "^//" name)
  2860.             (setq name (substring name 1)))))
  2861.       
  2862.       ;; Now substitute the expanded name back into the overall filename.
  2863.       (ange-ftp-replace-name-component n name))
  2864.       
  2865.       ;; non-ange-ftp name.  Just expand normally.
  2866.       (if (eq (string-to-char n) ?/)
  2867.       (ange-ftp-real-expand-file-name n)
  2868.     (ange-ftp-real-expand-file-name
  2869.      (ange-ftp-real-file-name-nondirectory n)
  2870.      (ange-ftp-real-file-name-directory n))))))
  2871.  
  2872. (defun ange-ftp-expand-file-name (name &optional default)
  2873.   "Documented as original."
  2874.   (save-match-data
  2875.     (if (eq (string-to-char name) ?/)
  2876.     (while (cond ((string-match "[^:]+//" name) ;don't upset Apollo users
  2877.               (setq name (substring name (1- (match-end 0)))))
  2878.              ((string-match "/~" name)
  2879.               (setq name (substring name (1- (match-end 0))))))))
  2880.     (cond ((eq (string-to-char name) ?~)
  2881.        (ange-ftp-real-expand-file-name name))
  2882.       ((eq (string-to-char name) ?/)
  2883.        (ange-ftp-canonize-filename name))
  2884.       ((zerop (length name))
  2885.        (ange-ftp-canonize-filename (or default default-directory)))
  2886.       ((ange-ftp-canonize-filename
  2887.         (concat (file-name-as-directory (or default default-directory))
  2888.             name))))))
  2889.  
  2890. ;;; These are problems--they are currently not enabled.
  2891.  
  2892. (defvar ange-ftp-file-name-as-directory-alist nil
  2893.   "Association list of \( TYPE \. FUNC \) pairs.
  2894. FUNC converts a filename to a directory name for the operating
  2895. system TYPE.")
  2896.  
  2897. (defun ange-ftp-file-name-as-directory (name)
  2898.   "Documented as original."
  2899.   (let ((parsed (ange-ftp-ftp-name name)))
  2900.     (if parsed
  2901.     (if (string-equal (nth 2 parsed) "")
  2902.         name
  2903.       (funcall (or (cdr (assq
  2904.                  (ange-ftp-host-type (car parsed))
  2905.                  ange-ftp-file-name-as-directory-alist))
  2906.                'ange-ftp-real-file-name-as-directory)
  2907.            name))
  2908.       (ange-ftp-real-file-name-as-directory name))))
  2909.      
  2910. (defun ange-ftp-file-name-directory (name)
  2911.   "Documented as original."
  2912.   (let ((parsed (ange-ftp-ftp-name name)))
  2913.     (if parsed
  2914.     (let ((filename (nth 2 parsed)))
  2915.       (if (save-match-data
  2916.         (string-match "^~[^/]*$" filename))
  2917.           name
  2918.         (ange-ftp-replace-name-component
  2919.          name
  2920.          (ange-ftp-real-file-name-directory filename))))
  2921.       (ange-ftp-real-file-name-directory name))))
  2922.  
  2923. (defun ange-ftp-file-name-nondirectory (name)
  2924.   "Documented as original."
  2925.   (let ((parsed (ange-ftp-ftp-name name)))
  2926.     (if parsed
  2927.     (let ((filename (nth 2 parsed)))
  2928.       (if (save-match-data
  2929.         (string-match "^~[^/]*$" filename))
  2930.           ""
  2931.         (ange-ftp-real-file-name-nondirectory name)))
  2932.       (ange-ftp-real-file-name-nondirectory name))))
  2933.  
  2934. (defun ange-ftp-directory-file-name (dir)
  2935.   "Documented as original."
  2936.   (let ((parsed (ange-ftp-ftp-name dir)))
  2937.     (if parsed
  2938.     (ange-ftp-replace-name-component
  2939.        dir
  2940.        (ange-ftp-real-directory-file-name (nth 2 parsed)))
  2941.       (ange-ftp-real-directory-file-name dir))))
  2942.  
  2943.  
  2944. ;;; Hooks that handle Emacs primitives.
  2945.  
  2946. ;; Returns non-nil if should transfer FILE in binary mode.
  2947. (defun ange-ftp-binary-file (file)
  2948.   (save-match-data
  2949.     (string-match ange-ftp-binary-file-name-regexp file)))
  2950.  
  2951. (defun ange-ftp-write-region (start end filename &optional append visit)
  2952.   (setq filename (expand-file-name filename))
  2953.   (let ((parsed (ange-ftp-ftp-name filename)))
  2954.     (if parsed
  2955.     (let* ((host (nth 0 parsed))
  2956.            (user (nth 1 parsed))
  2957.            (name (ange-ftp-quote-string (nth 2 parsed)))
  2958.            (temp (ange-ftp-make-tmp-name host))
  2959.            (binary (or (ange-ftp-binary-file filename)
  2960.                (eq (ange-ftp-host-type host user) 'unix)))
  2961.            (cmd (if append 'append 'put))
  2962.            (abbr (ange-ftp-abbreviate-filename filename)))
  2963.       (unwind-protect
  2964.           (progn
  2965.         (let ((executing-kbd-macro t)
  2966.               (filename (buffer-file-name))
  2967.               (mod-p (buffer-modified-p)))
  2968.           (unwind-protect
  2969.               (ange-ftp-real-write-region start end temp nil visit)
  2970.             ;; cleanup forms
  2971.             (setq buffer-file-name filename)
  2972.             (set-buffer-modified-p mod-p)))
  2973.         (if binary
  2974.             (ange-ftp-set-binary-mode host user))
  2975.  
  2976.         ;; tell the process filter what size the transfer will be.
  2977.         (let ((attr (file-attributes temp)))
  2978.           (if attr
  2979.               (ange-ftp-set-xfer-size host user (nth 7 attr))))
  2980.  
  2981.         ;; put or append the file.
  2982.         (let ((result (ange-ftp-send-cmd host user
  2983.                          (list cmd temp name)
  2984.                          (format "Writing %s" abbr))))
  2985.           (or (car result)
  2986.               (signal 'ftp-error
  2987.                   (list
  2988.                    "Opening output file"
  2989.                    (format "FTP Error: \"%s\"" (cdr result))
  2990.                    filename)))))
  2991.         (ange-ftp-del-tmp-name temp)
  2992.         (if binary 
  2993.         (ange-ftp-set-ascii-mode host user)))
  2994.       (if (eq visit t)
  2995.           (progn
  2996.         (set-visited-file-modtime '(0 0))
  2997.         (ange-ftp-set-buffer-mode)
  2998.         (setq buffer-file-name filename)
  2999.         (set-buffer-modified-p nil)))
  3000.       (ange-ftp-message "Wrote %s" abbr)
  3001.       (ange-ftp-add-file-entry filename))
  3002.       (ange-ftp-real-write-region start end filename append visit))))
  3003.  
  3004. (defun ange-ftp-insert-file-contents (filename &optional visit beg end replace)
  3005.   (barf-if-buffer-read-only)
  3006.   (setq filename (expand-file-name filename))
  3007.   (let ((parsed (ange-ftp-ftp-name filename)))
  3008.     (if parsed
  3009.     (progn
  3010.       (if visit
  3011.           (setq buffer-file-name filename))
  3012.       (if (or (file-exists-p filename)
  3013.           (progn
  3014.             (setq ange-ftp-ls-cache-file nil)
  3015.             (ange-ftp-del-hash-entry (file-name-directory filename)
  3016.                          ange-ftp-files-hashtable)
  3017.             (file-exists-p filename)))
  3018.           (let* ((host (nth 0 parsed))
  3019.              (user (nth 1 parsed))
  3020.              (name (ange-ftp-quote-string (nth 2 parsed)))
  3021.              (temp (ange-ftp-make-tmp-name host))
  3022.              (binary (or (ange-ftp-binary-file filename)
  3023.                  (eq (ange-ftp-host-type host user) 'unix)))
  3024.              (abbr (ange-ftp-abbreviate-filename filename))
  3025.              size)
  3026.         (unwind-protect
  3027.             (progn
  3028.               (if binary
  3029.               (ange-ftp-set-binary-mode host user))
  3030.               (let ((result (ange-ftp-send-cmd host user
  3031.                           (list 'get name temp)
  3032.                           (format "Retrieving %s" abbr))))
  3033.             (or (car result)
  3034.                 (signal 'ftp-error
  3035.                     (list
  3036.                      "Opening input file"
  3037.                      (format "FTP Error: \"%s\"" (cdr result))
  3038.                      filename))))
  3039.               (if (or (ange-ftp-real-file-readable-p temp)
  3040.                   (sleep-for ange-ftp-retry-time)
  3041.                   ;; Wait for file to hopefully appear.
  3042.                   (ange-ftp-real-file-readable-p temp))
  3043.               (setq
  3044.                size
  3045.                (nth 1 (ange-ftp-real-insert-file-contents
  3046.                    temp visit beg end replace)))
  3047.             (signal 'ftp-error
  3048.                 (list
  3049.                  "Opening input file:"
  3050.                  (format
  3051.                   "FTP Error: %s not arrived or readable"
  3052.                   filename)))))
  3053.           (if binary
  3054.               (ange-ftp-set-ascii-mode host user))
  3055.           (ange-ftp-del-tmp-name temp))
  3056.         (if visit
  3057.             (progn
  3058.               (set-visited-file-modtime '(0 0))
  3059.               (setq buffer-file-name filename)))
  3060.         (list filename size))
  3061.         (signal 'file-error
  3062.             (list 
  3063.              "Opening input file"
  3064.              filename))))
  3065.       (ange-ftp-real-insert-file-contents filename visit beg end replace))))
  3066.  
  3067. (defun ange-ftp-expand-symlink (file dir)
  3068.   (if (file-name-absolute-p file)
  3069.       (ange-ftp-replace-name-component dir file)
  3070.     (expand-file-name file dir)))
  3071.  
  3072. (defun ange-ftp-file-symlink-p (file)
  3073.   ;; call ange-ftp-expand-file-name rather than the normal
  3074.   ;; expand-file-name to stop loops when using a package that
  3075.   ;; redefines both file-symlink-p and expand-file-name.
  3076.   (setq file (ange-ftp-expand-file-name file))
  3077.   (if (ange-ftp-ftp-name file)
  3078.       (let ((file-ent
  3079.          (ange-ftp-get-hash-entry
  3080.           (ange-ftp-get-file-part file)
  3081.           (ange-ftp-get-files (file-name-directory file)))))
  3082.     (if (stringp file-ent)
  3083.         (if (file-name-absolute-p file-ent)
  3084.         (ange-ftp-replace-name-component
  3085.               (file-name-directory file) file-ent)
  3086.           file-ent)))
  3087.     (ange-ftp-real-file-symlink-p file)))
  3088.  
  3089. (defun ange-ftp-file-exists-p (name)
  3090.   (setq name (expand-file-name name))
  3091.   (if (ange-ftp-ftp-name name)
  3092.       (if (ange-ftp-file-entry-p name)
  3093.       (let ((file-ent (ange-ftp-get-file-entry name)))
  3094.         (if (stringp file-ent)
  3095.         (file-exists-p
  3096.          (ange-ftp-expand-symlink file-ent
  3097.                       (file-name-directory
  3098.                        (directory-file-name name))))
  3099.           t)))
  3100.     (ange-ftp-real-file-exists-p name)))
  3101.  
  3102. (defun ange-ftp-file-directory-p (name)
  3103.   (setq name (expand-file-name name))
  3104.   (if (ange-ftp-ftp-name name)
  3105.       ;; We do a file-name-as-directory on name here because some
  3106.       ;; machines (VMS) use a .DIR to indicate the filename associated
  3107.       ;; with a directory. This needs to be canonicalized.
  3108.       (let ((file-ent (ange-ftp-get-file-entry
  3109.                (ange-ftp-file-name-as-directory name))))
  3110.     (if (stringp file-ent)
  3111.         (file-directory-p
  3112.          (ange-ftp-expand-symlink file-ent
  3113.                       (file-name-directory
  3114.                        (directory-file-name name))))
  3115.       file-ent))
  3116.     (ange-ftp-real-file-directory-p name)))
  3117.  
  3118. (defun ange-ftp-directory-files (directory &optional full match
  3119.                        &rest v19-args)
  3120.   (setq directory (expand-file-name directory))
  3121.   (if (ange-ftp-ftp-name directory)
  3122.       (progn
  3123.     (ange-ftp-barf-if-not-directory directory)
  3124.     (let ((tail (ange-ftp-hash-table-keys
  3125.              (ange-ftp-get-files directory)))
  3126.           files f)
  3127.       (setq directory (file-name-as-directory directory))
  3128.       (save-match-data
  3129.         (while tail
  3130.           (setq f (car tail)
  3131.             tail (cdr tail))
  3132.           (if (or (not match) (string-match match f))
  3133.           (setq files
  3134.             (cons (if full (concat directory f) f) files)))))
  3135.       (nreverse files)))
  3136.     (apply 'ange-ftp-real-directory-files directory full match v19-args)))
  3137.  
  3138. (defun ange-ftp-file-attributes (file)
  3139.   (setq file (expand-file-name file))
  3140.   (let ((parsed (ange-ftp-ftp-name file)))
  3141.     (if parsed
  3142.     (let ((part (ange-ftp-get-file-part file))
  3143.           (files (ange-ftp-get-files (file-name-directory file))))
  3144.       (if (ange-ftp-hash-entry-exists-p part files)
  3145.           (let ((host (nth 0 parsed))
  3146.             (user (nth 1 parsed))
  3147.             (name (nth 2 parsed))
  3148.             (dirp (ange-ftp-get-hash-entry part files)))
  3149.         (list (if (and (stringp dirp) (file-name-absolute-p dirp))
  3150.               (ange-ftp-expand-symlink dirp
  3151.                            (file-name-directory file))
  3152.             dirp)        ;0 file type
  3153.               -1        ;1 link count
  3154.               -1        ;2 uid
  3155.               -1        ;3 gid
  3156.               '(0 0)        ;4 atime
  3157.               '(0 0)        ;5 mtime
  3158.               '(0 0)        ;6 ctime
  3159.               -1        ;7 size
  3160.               (concat (if (stringp dirp) "l" (if dirp "d" "-"))
  3161.                   "?????????") ;8 mode
  3162.               nil        ;9 gid weird
  3163.               ;; Hack to give remote files a unique "inode number".
  3164.               ;; It's actually the sum of the characters in its name.
  3165.               (apply '+ (nconc (mapcar 'identity host)
  3166.                        (mapcar 'identity user)
  3167.                        (mapcar 'identity
  3168.                            (directory-file-name name))))
  3169.               -1        ;11 device number [v19 only]
  3170.               ))))
  3171.       (ange-ftp-real-file-attributes file))))
  3172.  
  3173. (defun ange-ftp-file-writable-p (file)
  3174.   (setq file (expand-file-name file))
  3175.   (if (ange-ftp-ftp-name file)
  3176.       (or (file-exists-p file)        ;guess here for speed
  3177.       (file-directory-p (file-name-directory file)))
  3178.     (ange-ftp-real-file-writable-p file)))
  3179.  
  3180. (defun ange-ftp-file-readable-p (file)
  3181.   (setq file (expand-file-name file))
  3182.   (if (ange-ftp-ftp-name file)
  3183.       (file-exists-p file)
  3184.     (ange-ftp-real-file-readable-p file)))
  3185.  
  3186. (defun ange-ftp-file-executable-p (file)
  3187.   (setq file (expand-file-name file))
  3188.   (if (ange-ftp-ftp-name file)
  3189.       (file-exists-p file)
  3190.     (ange-ftp-real-file-executable-p file)))
  3191.  
  3192. (defun ange-ftp-delete-file (file)
  3193.   (interactive "fDelete file: ")
  3194.   (setq file (expand-file-name file))
  3195.   (let ((parsed (ange-ftp-ftp-name file)))
  3196.     (if parsed
  3197.     (let* ((host (nth 0 parsed))
  3198.            (user (nth 1 parsed))
  3199.            (name (ange-ftp-quote-string (nth 2 parsed)))
  3200.            (abbr (ange-ftp-abbreviate-filename file))
  3201.            (result (ange-ftp-send-cmd host user
  3202.                       (list 'delete name)
  3203.                       (format "Deleting %s" abbr))))
  3204.       (or (car result)
  3205.           (signal 'ftp-error
  3206.               (list
  3207.                "Removing old name"
  3208.                (format "FTP Error: \"%s\"" (cdr result))
  3209.                file)))
  3210.       (ange-ftp-delete-file-entry file))
  3211.       (ange-ftp-real-delete-file file))))
  3212.  
  3213. (defun ange-ftp-verify-visited-file-modtime (buf)
  3214.   (let ((name (buffer-file-name buf)))
  3215.     (if (and (stringp name) (ange-ftp-ftp-name name))
  3216.     t
  3217.       (ange-ftp-real-verify-visited-file-modtime buf))))
  3218.  
  3219. ;;;; ------------------------------------------------------------
  3220. ;;;; File copying support... totally re-written 6/24/92.
  3221. ;;;; ------------------------------------------------------------
  3222.  
  3223. (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
  3224.   (if (file-exists-p absname)
  3225.       (if (not interactive)
  3226.       (signal 'file-already-exists (list absname))
  3227.     (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
  3228.                       absname querystring)))
  3229.         (signal 'file-already-exists (list absname))))))
  3230.  
  3231. ;; async local copy commented out for now since I don't seem to get
  3232. ;; the process sentinel called for some processes.
  3233. ;;
  3234. ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
  3235. ;;                         keep-date cont)
  3236. ;;   "Kludge to copy a local file and call a continuation when the copy
  3237. ;; finishes."
  3238. ;;   ;; check to see if we can overwrite
  3239. ;;   (if (or (not ok-if-already-exists)
  3240. ;;       (numberp ok-if-already-exists))
  3241. ;;       (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3242. ;;                          (numberp ok-if-already-exists)))
  3243. ;;   (let ((proc (start-process " *copy*"
  3244. ;;                  (generate-new-buffer "*copy*")
  3245. ;;                  "cp"
  3246. ;;                  filename
  3247. ;;                  newname))
  3248. ;;     res)
  3249. ;;     (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel))
  3250. ;;     (process-kill-without-query proc)
  3251. ;;     (save-excursion
  3252. ;;       (set-buffer (process-buffer proc))
  3253. ;;       (make-variable-buffer-local 'copy-cont)
  3254. ;;       (setq copy-cont cont))))
  3255. ;; 
  3256. ;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
  3257. ;;   (save-excursion
  3258. ;;     (set-buffer (process-buffer proc))
  3259. ;;     (let ((cont copy-cont)
  3260. ;;       (result (buffer-string)))
  3261. ;;       (unwind-protect
  3262. ;;       (if (and (string-equal status "finished\n")
  3263. ;;            (zerop (length result)))
  3264. ;;           (ange-ftp-call-cont cont t nil)
  3265. ;;         (ange-ftp-call-cont cont
  3266. ;;                 nil
  3267. ;;                 (if (zerop (length result))
  3268. ;;                     (substring status 0 -1)
  3269. ;;                   (substring result 0 -1))))
  3270. ;;     (kill-buffer (current-buffer))))))
  3271.  
  3272. ;; this is the extended version of ange-ftp-copy-file-internal that works
  3273. ;; asynchronously if asked nicely.
  3274. (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
  3275.                          keep-date &optional msg cont nowait)
  3276.   (setq filename (expand-file-name filename)
  3277.     newname (expand-file-name newname))
  3278.  
  3279.   ;; canonicalize newname if a directory.
  3280.   (if (file-directory-p newname)
  3281.       (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
  3282.  
  3283.   (let ((f-parsed (ange-ftp-ftp-name filename))
  3284.     (t-parsed (ange-ftp-ftp-name newname)))
  3285.  
  3286.     ;; local file to local file copy?
  3287.     (if (and (not f-parsed) (not t-parsed))
  3288.     (progn
  3289.       (ange-ftp-real-copy-file filename newname ok-if-already-exists
  3290.                    keep-date)
  3291.       (if cont
  3292.           (ange-ftp-call-cont cont t "Copied locally")))
  3293.       ;; one or both files are remote.
  3294.       (let* ((f-host (and f-parsed (nth 0 f-parsed)))
  3295.          (f-user (and f-parsed (nth 1 f-parsed)))
  3296.          (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
  3297.          (f-abbr (ange-ftp-abbreviate-filename filename))
  3298.          (t-host (and t-parsed (nth 0 t-parsed)))
  3299.          (t-user (and t-parsed (nth 1 t-parsed)))
  3300.          (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
  3301.          (t-abbr (ange-ftp-abbreviate-filename newname filename))
  3302.          (binary (or (ange-ftp-binary-file filename)
  3303.              (ange-ftp-binary-file newname)
  3304.              (and (eq (ange-ftp-host-type f-host f-user) 'unix)
  3305.                   (eq (ange-ftp-host-type t-host t-user) 'unix))))
  3306.          temp1
  3307.          temp2)
  3308.  
  3309.     ;; check to see if we can overwrite
  3310.     (if (or (not ok-if-already-exists)
  3311.         (numberp ok-if-already-exists))
  3312.         (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3313.                            (numberp ok-if-already-exists)))
  3314.  
  3315.     ;; do the copying.
  3316.     (if f-parsed
  3317.         
  3318.         ;; filename was remote.
  3319.         (progn
  3320.           (if (or (ange-ftp-use-gateway-p f-host)
  3321.               t-parsed)
  3322.           ;; have to use intermediate file if we are getting via
  3323.           ;; gateway machine or we are doing a remote to remote copy.
  3324.           (setq temp1 (ange-ftp-make-tmp-name f-host)))
  3325.           
  3326.           (if binary
  3327.           (ange-ftp-set-binary-mode f-host f-user))
  3328.  
  3329.           (ange-ftp-send-cmd
  3330.            f-host
  3331.            f-user
  3332.            (list 'get f-name (or temp1 newname))
  3333.            (or msg
  3334.            (if (and temp1 t-parsed)
  3335.                (format "Getting %s" f-abbr)
  3336.              (format "Copying %s to %s" f-abbr t-abbr)))
  3337.            (list (function ange-ftp-cf1)
  3338.              filename newname binary msg
  3339.              f-parsed f-host f-user f-name f-abbr
  3340.              t-parsed t-host t-user t-name t-abbr
  3341.              temp1 temp2 cont nowait)
  3342.            nowait))
  3343.  
  3344.       ;; filename wasn't remote.  newname must be remote.  call the
  3345.       ;; function which does the remainder of the copying work.
  3346.       (ange-ftp-cf1 t nil
  3347.             filename newname binary msg
  3348.             f-parsed f-host f-user f-name f-abbr
  3349.             t-parsed t-host t-user t-name t-abbr
  3350.             nil nil cont nowait))))))
  3351.  
  3352. ;; next part of copying routine.
  3353. (defun ange-ftp-cf1 (result line
  3354.                 filename newname binary msg
  3355.                 f-parsed f-host f-user f-name f-abbr
  3356.                 t-parsed t-host t-user t-name t-abbr
  3357.                 temp1 temp2 cont nowait)
  3358.   (if line
  3359.       ;; filename must have been remote, and we must have just done a GET.
  3360.       (unwind-protect
  3361.       (or result
  3362.           ;; GET failed for some reason.  Clean up and get out.
  3363.           (progn
  3364.         (and temp1 (ange-ftp-del-tmp-name temp1))
  3365.         (or cont
  3366.             (signal 'ftp-error (list "Opening input file"
  3367.                          (format "FTP Error: \"%s\"" line)
  3368.                          filename)))))
  3369.     ;; cleanup
  3370.     (if binary
  3371.         (ange-ftp-set-ascii-mode f-host f-user))))
  3372.  
  3373.   (if result
  3374.       ;; We now have to copy either temp1 or filename to newname.
  3375.       (if t-parsed
  3376.       
  3377.       ;; newname was remote.
  3378.       (progn
  3379.         (if (ange-ftp-use-gateway-p t-host)
  3380.         (setq temp2 (ange-ftp-make-tmp-name t-host)))
  3381.         
  3382.         ;; make sure data is moved into the right place for the
  3383.         ;; outgoing transfer.  gateway temporary files complicate
  3384.         ;; things nicely.
  3385.         (if temp1
  3386.         (if temp2
  3387.             (if (string-equal temp1 temp2)
  3388.             (setq temp1 nil)
  3389.               (ange-ftp-real-copy-file temp1 temp2 t))
  3390.           (setq temp2 temp1 temp1 nil))
  3391.           (if temp2
  3392.           (ange-ftp-real-copy-file filename temp2 t)))
  3393.         
  3394.         (if binary
  3395.         (ange-ftp-set-binary-mode t-host t-user))
  3396.  
  3397.         ;; tell the process filter what size the file is.
  3398.         (let ((attr (file-attributes (or temp2 filename))))
  3399.           (if attr
  3400.           (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
  3401.  
  3402.         (ange-ftp-send-cmd
  3403.          t-host
  3404.          t-user
  3405.          (list 'put (or temp2 filename) t-name)
  3406.          (or msg
  3407.          (if (and temp2 f-parsed)
  3408.              (format "Putting %s" newname)
  3409.            (format "Copying %s to %s" f-abbr t-abbr)))
  3410.          (list (function ange-ftp-cf2)
  3411.            newname t-host t-user binary temp1 temp2 cont)
  3412.          nowait))
  3413.     
  3414.     ;; newname wasn't remote.
  3415.     (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
  3416.  
  3417.     ;; first copy failed, tell caller
  3418.     (ange-ftp-call-cont cont result line)))
  3419.  
  3420. ;; last part of copying routine.
  3421. (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
  3422.   (unwind-protect
  3423.       (if line
  3424.       ;; result from doing a local to remote copy.
  3425.       (unwind-protect
  3426.           (progn
  3427.         (or result
  3428.             (or cont
  3429.             (signal 'ftp-error
  3430.                 (list "Opening output file"
  3431.                       (format "FTP Error: \"%s\"" line)
  3432.                       newname))))
  3433.         
  3434.         (ange-ftp-add-file-entry newname))
  3435.         
  3436.         ;; cleanup.
  3437.         (if binary
  3438.         (ange-ftp-set-ascii-mode t-host t-user)))
  3439.           
  3440.     ;; newname was local.
  3441.     (if temp1
  3442.         (ange-ftp-real-copy-file temp1 newname t)))
  3443.       
  3444.     ;; clean up
  3445.     (and temp1 (ange-ftp-del-tmp-name temp1))
  3446.     (and temp2 (ange-ftp-del-tmp-name temp2))
  3447.     (ange-ftp-call-cont cont result line)))
  3448.  
  3449. (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
  3450.                     keep-date)
  3451.   (interactive "fCopy file: \nFCopy %s to file: \np")
  3452.   (ange-ftp-copy-file-internal filename
  3453.                    newname
  3454.                    ok-if-already-exists
  3455.                    keep-date
  3456.                    nil
  3457.                    nil
  3458.                    (interactive-p)))
  3459.  
  3460. ;;;; ------------------------------------------------------------
  3461. ;;;; File renaming support.
  3462. ;;;; ------------------------------------------------------------
  3463.  
  3464. (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed)
  3465.   "Rename remote file FILE to remote file NEWNAME."
  3466.   (let ((f-host (nth 0 f-parsed))
  3467.     (f-user (nth 1 f-parsed))
  3468.     (t-host (nth 0 t-parsed))
  3469.     (t-user (nth 1 t-parsed)))
  3470.     (if (and (string-equal f-host t-host)
  3471.          (string-equal f-user t-user))
  3472.     (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed)))
  3473.            (t-name (ange-ftp-quote-string (nth 2 t-parsed)))
  3474.            (cmd (list 'rename f-name t-name))
  3475.            (fabbr (ange-ftp-abbreviate-filename filename))
  3476.            (nabbr (ange-ftp-abbreviate-filename newname filename))
  3477.            (result (ange-ftp-send-cmd f-host f-user cmd
  3478.                       (format "Renaming %s to %s"
  3479.                           fabbr
  3480.                           nabbr))))
  3481.       (or (car result)
  3482.           (signal 'ftp-error
  3483.               (list
  3484.                "Renaming"
  3485.                (format "FTP Error: \"%s\"" (cdr result))
  3486.                filename
  3487.                newname)))
  3488.       (ange-ftp-add-file-entry newname)
  3489.       (ange-ftp-delete-file-entry filename))
  3490.       (ange-ftp-copy-file-internal filename newname t nil)
  3491.       (delete-file filename))))
  3492.  
  3493. (defun ange-ftp-rename-local-to-remote (filename newname)
  3494.   "Rename local FILENAME to remote file NEWNAME."
  3495.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3496.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3497.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3498.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3499.     (let (ange-ftp-process-verbose)
  3500.       (delete-file filename))))
  3501.  
  3502. (defun ange-ftp-rename-remote-to-local (filename newname)
  3503.   "Rename remote file FILENAME to local file NEWNAME."
  3504.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3505.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3506.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3507.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3508.     (let (ange-ftp-process-verbose)
  3509.       (delete-file filename))))
  3510.  
  3511. (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
  3512.   (interactive "fRename file: \nFRename %s to file: \np")
  3513.   (setq filename (expand-file-name filename))
  3514.   (setq newname (expand-file-name newname))
  3515.   (let* ((f-parsed (ange-ftp-ftp-name filename))
  3516.      (t-parsed (ange-ftp-ftp-name newname)))
  3517.     (if (and (or f-parsed t-parsed)
  3518.          (or (not ok-if-already-exists)
  3519.          (numberp ok-if-already-exists)))
  3520.     (ange-ftp-barf-or-query-if-file-exists
  3521.      newname
  3522.      "rename to it"
  3523.      (numberp ok-if-already-exists)))
  3524.     (if f-parsed
  3525.     (if t-parsed
  3526.         (ange-ftp-rename-remote-to-remote filename newname f-parsed
  3527.                           t-parsed)
  3528.       (ange-ftp-rename-remote-to-local filename newname))
  3529.       (if t-parsed
  3530.       (ange-ftp-rename-local-to-remote filename newname)
  3531.     (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
  3532.  
  3533. ;;;; ------------------------------------------------------------
  3534. ;;;; File name completion support.
  3535. ;;;; ------------------------------------------------------------
  3536.  
  3537. ;; If the file entry SYM is a symlink, returns whether its file exists.
  3538. ;; Note that `ange-ftp-this-dir' is used as a free variable.
  3539. (defun ange-ftp-file-entry-active-p (sym)
  3540.   (let ((val (get sym 'val)))
  3541.     (or (not (stringp val))
  3542.     (file-exists-p (ange-ftp-expand-symlink val ange-ftp-this-dir)))))
  3543.  
  3544. ;; If the file entry is not a directory (nor a symlink pointing to a directory)
  3545. ;; returns whether the file (or file pointed to by the symlink) is ignored
  3546. ;; by completion-ignored-extensions.
  3547. ;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern'
  3548. ;; are used as free variables.
  3549. (defun ange-ftp-file-entry-not-ignored-p (sym)
  3550.   (let ((val (get sym 'val))
  3551.     (symname (symbol-name sym)))
  3552.     (if (stringp val)
  3553.     (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir)))
  3554.       (or (file-directory-p file)
  3555.           (and (file-exists-p file)
  3556.            (not (string-match ange-ftp-completion-ignored-pattern
  3557.                       symname)))))
  3558.       (or val ; is a directory name
  3559.       (not (string-match ange-ftp-completion-ignored-pattern symname))))))
  3560.  
  3561. (defun ange-ftp-file-name-all-completions (file dir)
  3562.   (let ((ange-ftp-this-dir (expand-file-name dir)))
  3563.     (if (ange-ftp-ftp-name ange-ftp-this-dir)
  3564.     (progn
  3565.       (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
  3566.       (setq ange-ftp-this-dir
  3567.         (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))
  3568.       (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
  3569.          (completions
  3570.           (all-completions file tbl
  3571.                    (function ange-ftp-file-entry-active-p))))
  3572.  
  3573.         ;; see whether each matching file is a directory or not...
  3574.         (mapcar
  3575.          (function
  3576.           (lambda (file)
  3577.         (let ((ent (ange-ftp-get-hash-entry file tbl)))
  3578.           (if (and ent
  3579.                (or (not (stringp ent))
  3580.                    (file-directory-p
  3581.                 (ange-ftp-expand-symlink ent
  3582.                              ange-ftp-this-dir))))
  3583.               (concat file "/")
  3584.             file))))
  3585.          completions)))
  3586.  
  3587.       (if (string-equal "/" ange-ftp-this-dir)
  3588.       (nconc (all-completions file (ange-ftp-generate-root-prefixes))
  3589.          (ange-ftp-real-file-name-all-completions file
  3590.                               ange-ftp-this-dir))
  3591.     (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir)))))
  3592.  
  3593. (defun ange-ftp-file-name-completion (file dir)
  3594.   (let ((ange-ftp-this-dir (expand-file-name dir)))
  3595.     (if (ange-ftp-ftp-name ange-ftp-this-dir)
  3596.     (progn
  3597.       (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
  3598.       (if (equal file "")
  3599.           ""
  3600.         (setq ange-ftp-this-dir
  3601.           (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))    ;real?
  3602.         (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
  3603.            (ange-ftp-completion-ignored-pattern
  3604.             (mapconcat (function
  3605.                 (lambda (s) (if (stringp s)
  3606.                         (concat (regexp-quote s) "$")
  3607.                           "/"))) ; / never in filename
  3608.                    completion-ignored-extensions
  3609.                    "\\|")))
  3610.           (save-match-data
  3611.         (or (ange-ftp-file-name-completion-1
  3612.              file tbl ange-ftp-this-dir
  3613.              (function ange-ftp-file-entry-not-ignored-p))
  3614.             (ange-ftp-file-name-completion-1
  3615.              file tbl ange-ftp-this-dir
  3616.              (function ange-ftp-file-entry-active-p)))))))
  3617.  
  3618.       (if (string-equal "/" ange-ftp-this-dir)
  3619.       (try-completion
  3620.        file
  3621.        (nconc (ange-ftp-generate-root-prefixes)
  3622.           (mapcar 'list
  3623.               (ange-ftp-real-file-name-all-completions file "/"))))
  3624.     (ange-ftp-real-file-name-completion file ange-ftp-this-dir)))))
  3625.  
  3626.  
  3627. (defun ange-ftp-file-name-completion-1 (file tbl dir predicate)
  3628.   (let ((bestmatch (try-completion file tbl predicate)))
  3629.     (if bestmatch
  3630.     (if (eq bestmatch t)
  3631.         (if (file-directory-p (expand-file-name file dir))
  3632.         (concat file "/")
  3633.           t)
  3634.       (if (and (eq (try-completion bestmatch tbl predicate) t)
  3635.            (file-directory-p
  3636.             (expand-file-name bestmatch dir)))
  3637.           (concat bestmatch "/")
  3638.         bestmatch)))))
  3639.  
  3640. ;; Put these lines uncommmented in your .emacs if you want C-r to refresh
  3641. ;; ange-ftp's cache whilst doing filename completion.
  3642. ;;
  3643. ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
  3644. ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
  3645.  
  3646. ;; Force a re-read of the directory DIR.  If DIR is omitted then it defaults
  3647. ;; to the directory part of the contents of the current buffer.
  3648. (defun ange-ftp-re-read-dir (&optional dir)
  3649.   (interactive)
  3650.   (if dir
  3651.       (setq dir (expand-file-name dir))
  3652.     (setq dir (file-name-directory (expand-file-name (buffer-string)))))
  3653.   (if (ange-ftp-ftp-name dir)
  3654.       (progn
  3655.     (setq ange-ftp-ls-cache-file nil)
  3656.     (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable)
  3657.     (ange-ftp-get-files dir t))))
  3658.  
  3659. (defun ange-ftp-make-directory (dir &optional parents)
  3660.   (interactive (list (expand-file-name (read-file-name "Make directory: "))))
  3661.   (if parents
  3662.       (let ((parent (file-name-directory (directory-file-name dir))))
  3663.     (or (file-exists-p parent)
  3664.         (ange-ftp-make-directory parent parents))))
  3665.   (if (file-exists-p dir)
  3666.       (error "Cannot make directory %s: file already exists" dir)
  3667.     (let ((parsed (ange-ftp-ftp-name dir)))
  3668.       (if parsed
  3669.       (let* ((host (nth 0 parsed))
  3670.          (user (nth 1 parsed))
  3671.          ;; Some ftp's on unix machines (at least on Suns)
  3672.          ;; insist that mkdir take a filename, and not a
  3673.          ;; directory-name name as an arg. Argh!! This is a bug.
  3674.          ;; Non-unix machines will probably always insist
  3675.          ;; that mkdir takes a directory-name as an arg
  3676.          ;; (as the ftp man page says it should).
  3677.          (name (ange-ftp-quote-string
  3678.             (if (eq (ange-ftp-host-type host) 'unix)
  3679.                 (ange-ftp-real-directory-file-name (nth 2 parsed))
  3680.               (ange-ftp-real-file-name-as-directory
  3681.                (nth 2 parsed)))))
  3682.          (abbr (ange-ftp-abbreviate-filename dir))
  3683.          (result (ange-ftp-send-cmd host user
  3684.                         (list 'mkdir name)
  3685.                         (format "Making directory %s"
  3686.                             abbr))))
  3687.         (or (car result)
  3688.         (ange-ftp-error host user
  3689.                 (format "Could not make directory %s: %s"
  3690.                     dir
  3691.                     (cdr result))))
  3692.         (ange-ftp-add-file-entry dir t))
  3693.     (ange-ftp-real-make-directory dir)))))
  3694.  
  3695. (defun ange-ftp-delete-directory (dir)
  3696.   (if (file-directory-p dir)
  3697.       (let ((parsed (ange-ftp-ftp-name dir)))
  3698.     (if parsed
  3699.         (let* ((host (nth 0 parsed))
  3700.            (user (nth 1 parsed))
  3701.            ;; Some ftp's on unix machines (at least on Suns)
  3702.            ;; insist that rmdir take a filename, and not a
  3703.            ;; directory-name name as an arg. Argh!! This is a bug.
  3704.            ;; Non-unix machines will probably always insist
  3705.            ;; that rmdir takes a directory-name as an arg
  3706.            ;; (as the ftp man page says it should).
  3707.            (name (ange-ftp-quote-string
  3708.               (if (eq (ange-ftp-host-type host) 'unix)
  3709.                   (ange-ftp-real-directory-file-name
  3710.                    (nth 2 parsed))
  3711.                 (ange-ftp-real-file-name-as-directory
  3712.                  (nth 2 parsed)))))
  3713.           (abbr (ange-ftp-abbreviate-filename dir))
  3714.           (result (ange-ftp-send-cmd host user
  3715.                          (list 'rmdir name)
  3716.                          (format "Removing directory %s"
  3717.                              abbr))))
  3718.           (or (car result)
  3719.           (ange-ftp-error host user
  3720.                   (format "Could not remove directory %s: %s"
  3721.                       dir
  3722.                       (cdr result))))
  3723.           (ange-ftp-delete-file-entry dir t))
  3724.       (ange-ftp-real-delete-directory dir)))
  3725.     (error "Not a directory: %s" dir)))
  3726.  
  3727. ;; Make a local copy of FILE and return its name.
  3728.  
  3729. (defun ange-ftp-file-local-copy (file)
  3730.   (let* ((fn1 (expand-file-name file))
  3731.      (pa1 (ange-ftp-ftp-name fn1)))
  3732.     (if pa1
  3733.     (let ((tmp1 (ange-ftp-make-tmp-name (car pa1))))
  3734.       (ange-ftp-copy-file-internal fn1 tmp1 t nil
  3735.                        (format "Getting %s" fn1))
  3736.       tmp1))))
  3737.  
  3738. (defun ange-ftp-load (file &optional noerror nomessage nosuffix)
  3739.   (if (ange-ftp-ftp-name file)
  3740.       (let ((tryfiles (if nosuffix
  3741.               (list file)
  3742.             (list (concat file ".elc") (concat file ".el") file)))
  3743.         copy)
  3744.     (while (and tryfiles (not copy))
  3745.       (condition-case error
  3746.           (setq copy (ange-ftp-file-local-copy (car tryfiles)))
  3747.         (ftp-error nil))
  3748.       (setq tryfiles (cdr tryfiles)))
  3749.     (if copy
  3750.         (unwind-protect
  3751.         (funcall 'load copy noerror nomessage nosuffix)
  3752.           (delete-file copy))
  3753.       (or noerror
  3754.           (signal 'file-error (list "Cannot open load file" file)))))
  3755.     (ange-ftp-real-load file noerror nomessage nosuffix)))
  3756.  
  3757. ;; Calculate default-unhandled-directory for a given ange-ftp buffer.
  3758. (defun ange-ftp-unhandled-file-name-directory (filename)
  3759.   (file-name-directory ange-ftp-tmp-name-template))
  3760.  
  3761.  
  3762. ;; Need the following functions for making filenames of compressed
  3763. ;; files, because some OS's (unlike UNIX) do not allow a filename to
  3764. ;; have two extensions.
  3765.  
  3766. (defvar ange-ftp-make-compressed-filename-alist nil
  3767.   "Alist of host-type-specific functions to process file names for compression.
  3768. Each element has the form (TYPE . FUNC).
  3769. FUNC should take one argument, a file name, and return a list
  3770. of the form (COMPRESSING NEWNAME).
  3771. COMPRESSING should be t if the specified file should be compressed, 
  3772. and nil if it should be uncompressed (that is, if it is a compressed file).
  3773. NEWNAME should be the name to give the new compressed or uncompressed file.")
  3774.  
  3775. (defun ange-ftp-dired-compress-file (name)
  3776.   (let ((parsed (ange-ftp-ftp-name name))
  3777.     conversion-func)
  3778.     (if (and parsed
  3779.          (setq conversion-func
  3780.            (cdr (assq (ange-ftp-host-type (car parsed))
  3781.                   ange-ftp-make-compressed-filename-alist))))
  3782.     (let* ((decision
  3783.         (save-match-data (funcall conversion-func name)))
  3784.            (compressing (car decision))
  3785.            (newfile (nth 1 decision)))
  3786.       (if compressing
  3787.           (ange-ftp-compress name newfile)
  3788.         (ange-ftp-uncompress name newfile)))
  3789.       (let (file-name-handler-alist)
  3790.     (dired-compress-file name)))))
  3791.  
  3792. ;; Copy FILE to this machine, compress it, and copy out to NFILE.
  3793. (defun ange-ftp-compress (file nfile)
  3794.   (let* ((parsed (ange-ftp-ftp-name file))
  3795.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  3796.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  3797.      (abbr (ange-ftp-abbreviate-filename file))
  3798.      (nabbr (ange-ftp-abbreviate-filename nfile))
  3799.      (msg1 (format "Getting %s" abbr))
  3800.      (msg2 (format "Putting %s" nabbr)))
  3801.     (unwind-protect
  3802.     (progn
  3803.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  3804.       (and ange-ftp-process-verbose
  3805.            (ange-ftp-message "Compressing %s..." abbr))
  3806.       (call-process-region (point)
  3807.                    (point)
  3808.                    shell-file-name
  3809.                    nil
  3810.                    t
  3811.                    nil
  3812.                    "-c"
  3813.                    (format "compress -f -c < %s > %s" tmp1 tmp2))
  3814.       (and ange-ftp-process-verbose
  3815.            (ange-ftp-message "Compressing %s...done" abbr))
  3816.       (if (zerop (buffer-size))
  3817.           (progn
  3818.         (let (ange-ftp-process-verbose)
  3819.           (delete-file file))
  3820.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  3821.       (ange-ftp-del-tmp-name tmp1)
  3822.       (ange-ftp-del-tmp-name tmp2))))
  3823.     
  3824. ;; Copy FILE to this machine, uncompress it, and copy out to NFILE.
  3825. (defun ange-ftp-uncompress (file nfile)
  3826.   (let* ((parsed (ange-ftp-ftp-name file))
  3827.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  3828.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  3829.      (abbr (ange-ftp-abbreviate-filename file))
  3830.      (nabbr (ange-ftp-abbreviate-filename nfile))
  3831.      (msg1 (format "Getting %s" abbr))
  3832.      (msg2 (format "Putting %s" nabbr))
  3833. ;;     ;; Cheap hack because of problems with binary file transfers from
  3834. ;;     ;; VMS hosts.
  3835. ;;     (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
  3836.      )
  3837.     (unwind-protect
  3838.     (progn
  3839.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  3840.       (and ange-ftp-process-verbose
  3841.            (ange-ftp-message "Uncompressing %s..." abbr))
  3842.       (call-process-region (point)
  3843.                    (point)
  3844.                    shell-file-name
  3845.                    nil
  3846.                    t
  3847.                    nil
  3848.                    "-c"
  3849.                    (format "uncompress -c < %s > %s" tmp1 tmp2))
  3850.       (and ange-ftp-process-verbose
  3851.            (ange-ftp-message "Uncompressing %s...done" abbr))
  3852.       (if (zerop (buffer-size))
  3853.           (progn
  3854.         (let (ange-ftp-process-verbose)
  3855.           (delete-file file))
  3856.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  3857.       (ange-ftp-del-tmp-name tmp1)
  3858.       (ange-ftp-del-tmp-name tmp2))))
  3859.  
  3860. (defun ange-ftp-find-backup-file-name (fn)
  3861.   ;; Either return the ordinary backup name, etc.,
  3862.   ;; or return nil meaning don't make a backup.
  3863.   (if ange-ftp-make-backup-files
  3864.       (ange-ftp-real-find-backup-file-name fn)))
  3865.  
  3866. ;;; Define the handler for special file names
  3867. ;;; that causes ange-ftp to be invoked.
  3868.  
  3869. ;;;###autoload
  3870. (defun ange-ftp-hook-function (operation &rest args)
  3871.   (let ((fn (get operation 'ange-ftp)))
  3872.     (if fn (apply fn args)
  3873.       (ange-ftp-run-real-handler operation args))))
  3874.  
  3875.  
  3876. ;;; This regexp takes care of real ange-ftp file names (with a slash
  3877. ;;; and colon).
  3878. ;;; Don't allow the host name to end in a period--some systems use /.:
  3879. ;;;###autoload
  3880. (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
  3881.     (setq file-name-handler-alist
  3882.       (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
  3883.         file-name-handler-alist)))
  3884.  
  3885. ;;; This regexp recognizes and absolute filenames with only one component,
  3886. ;;; for the sake of hostname completion.
  3887. ;;;###autoload
  3888. (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
  3889.     (setq file-name-handler-alist
  3890.       (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
  3891.         file-name-handler-alist)))
  3892.  
  3893. ;;; The above two forms are sufficient to cause this file to be loaded
  3894. ;;; if the user ever uses a file name with a colon in it.
  3895.  
  3896. ;;; This sets the mode 
  3897. (or (memq 'ange-ftp-set-buffer-mode find-file-hooks)
  3898.     (setq find-file-hooks
  3899.       (cons 'ange-ftp-set-buffer-mode find-file-hooks)))
  3900.  
  3901. ;;; Now say where to find the handlers for particular operations.
  3902.  
  3903. (put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory)
  3904. (put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory)
  3905. (put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory)
  3906. (put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name)
  3907. (put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name)
  3908. (put 'make-directory 'ange-ftp 'ange-ftp-make-directory)
  3909. (put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory)
  3910. (put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents)
  3911. (put 'directory-files 'ange-ftp 'ange-ftp-directory-files)
  3912. (put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p)
  3913. (put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p)
  3914. (put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p)
  3915. (put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p)
  3916. (put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p)
  3917. (put 'delete-file 'ange-ftp 'ange-ftp-delete-file)
  3918. (put 'read-file-name-internal 'ange-ftp 'ange-ftp-read-file-name-internal)
  3919. (put 'verify-visited-file-modtime 'ange-ftp
  3920.      'ange-ftp-verify-visited-file-modtime)
  3921. (put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p)
  3922. (put 'write-region 'ange-ftp 'ange-ftp-write-region)
  3923. (put 'backup-buffer 'ange-ftp 'ange-ftp-backup-buffer)
  3924. (put 'copy-file 'ange-ftp 'ange-ftp-copy-file)
  3925. (put 'rename-file 'ange-ftp 'ange-ftp-rename-file)
  3926. (put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes)
  3927. (put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions)
  3928. (put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion)
  3929. (put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory)
  3930. (put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy)
  3931. (put 'unhandled-file-name-directory 'ange-ftp
  3932.      'ange-ftp-unhandled-file-name-directory)
  3933. (put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions)
  3934. (put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache)
  3935. (put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file)
  3936. (put 'load 'ange-ftp 'ange-ftp-load)
  3937. (put 'find-backup-file-name 'ange-ftp 'ange-ftp-find-backup-file-name)
  3938.  
  3939. ;; Turn off truename processing to save time.
  3940. ;; Treat each name as its own truename.
  3941. (put 'file-truename 'ange-ftp 'identity)
  3942.  
  3943. ;; Turn off RCS/SCCS processing to save time.
  3944. ;; This returns nil for any file name as argument.
  3945. (put 'vc-registered 'ange-ftp 'null)
  3946.  
  3947. (put 'dired-call-process 'ange-ftp 'ange-ftp-dired-call-process)
  3948.  
  3949. ;;; Define ways of getting at unmodified Emacs primitives,
  3950. ;;; turning off our handler.
  3951.  
  3952. (defun ange-ftp-run-real-handler (operation args)
  3953.   (let ((inhibit-file-name-handlers
  3954.      (cons 'ange-ftp-hook-function
  3955.            (cons 'ange-ftp-completion-hook-function
  3956.              (and (eq inhibit-file-name-operation operation)
  3957.               inhibit-file-name-handlers))))
  3958.     (inhibit-file-name-operation operation))
  3959.     (apply operation args)))
  3960.  
  3961. (defun ange-ftp-real-file-name-directory (&rest args)
  3962.   (ange-ftp-run-real-handler 'file-name-directory args))
  3963. (defun ange-ftp-real-file-name-nondirectory (&rest args)
  3964.   (ange-ftp-run-real-handler 'file-name-nondirectory args))
  3965. (defun ange-ftp-real-file-name-as-directory (&rest args)
  3966.   (ange-ftp-run-real-handler 'file-name-as-directory args))
  3967. (defun ange-ftp-real-directory-file-name (&rest args)
  3968.   (ange-ftp-run-real-handler 'directory-file-name args))
  3969. (defun ange-ftp-real-expand-file-name (&rest args)
  3970.   (ange-ftp-run-real-handler 'expand-file-name args))
  3971. (defun ange-ftp-real-make-directory (&rest args)
  3972.   (ange-ftp-run-real-handler 'make-directory args))
  3973. (defun ange-ftp-real-delete-directory (&rest args)
  3974.   (ange-ftp-run-real-handler 'delete-directory args))
  3975. (defun ange-ftp-real-insert-file-contents (&rest args)
  3976.   (ange-ftp-run-real-handler 'insert-file-contents args))
  3977. (defun ange-ftp-real-directory-files (&rest args)
  3978.   (ange-ftp-run-real-handler 'directory-files args))
  3979. (defun ange-ftp-real-file-directory-p (&rest args)
  3980.   (ange-ftp-run-real-handler 'file-directory-p args))
  3981. (defun ange-ftp-real-file-writable-p (&rest args)
  3982.   (ange-ftp-run-real-handler 'file-writable-p args))
  3983. (defun ange-ftp-real-file-readable-p (&rest args)
  3984.   (ange-ftp-run-real-handler 'file-readable-p args))
  3985. (defun ange-ftp-real-file-executable-p (&rest args)
  3986.   (ange-ftp-run-real-handler 'file-executable-p args))
  3987. (defun ange-ftp-real-file-symlink-p (&rest args)
  3988.   (ange-ftp-run-real-handler 'file-symlink-p args))
  3989. (defun ange-ftp-real-delete-file (&rest args)
  3990.   (ange-ftp-run-real-handler 'delete-file args))
  3991. (defun ange-ftp-real-read-file-name-internal (&rest args)
  3992.   (ange-ftp-run-real-handler 'read-file-name-internal args))
  3993. (defun ange-ftp-real-verify-visited-file-modtime (&rest args)
  3994.   (ange-ftp-run-real-handler 'verify-visited-file-modtime args))
  3995. (defun ange-ftp-real-file-exists-p (&rest args)
  3996.   (ange-ftp-run-real-handler 'file-exists-p args))
  3997. (defun ange-ftp-real-write-region (&rest args)
  3998.   (ange-ftp-run-real-handler 'write-region args))
  3999. (defun ange-ftp-real-backup-buffer (&rest args)
  4000.   (ange-ftp-run-real-handler 'backup-buffer args))
  4001. (defun ange-ftp-real-copy-file (&rest args)
  4002.   (ange-ftp-run-real-handler 'copy-file args))
  4003. (defun ange-ftp-real-rename-file (&rest args)
  4004.   (ange-ftp-run-real-handler 'rename-file args))
  4005. (defun ange-ftp-real-file-attributes (&rest args)
  4006.   (ange-ftp-run-real-handler 'file-attributes args))
  4007. (defun ange-ftp-real-file-name-all-completions (&rest args)
  4008.   (ange-ftp-run-real-handler 'file-name-all-completions args))
  4009. (defun ange-ftp-real-file-name-completion (&rest args)
  4010.   (ange-ftp-run-real-handler 'file-name-completion args))
  4011. (defun ange-ftp-real-insert-directory (&rest args)
  4012.   (ange-ftp-run-real-handler 'insert-directory args))
  4013. (defun ange-ftp-real-file-name-sans-versions (&rest args)
  4014.   (ange-ftp-run-real-handler 'file-name-sans-versions args))
  4015. (defun ange-ftp-real-shell-command (&rest args)
  4016.   (ange-ftp-run-real-handler 'shell-command args))
  4017. (defun ange-ftp-real-load (&rest args)
  4018.   (ange-ftp-run-real-handler 'load args))
  4019. (defun ange-ftp-real-find-backup-file-name (&rest args)
  4020.   (ange-ftp-run-real-handler 'find-backup-file-name args))
  4021.  
  4022. ;; Here we support using dired on remote hosts.
  4023. ;; I have turned off the support for using dired on foreign directory formats.
  4024. ;; That involves too many unclean hooks.
  4025. ;; It would be cleaner to support such operations by 
  4026. ;; converting the foreign directory format to something dired can understand;
  4027. ;; something close to ls -l output.
  4028. ;; The logical place to do this is in the functions ange-ftp-parse-...-listing.
  4029.  
  4030. ;; Some of the old dired hooks would still be needed even if this is done.
  4031. ;; I have preserved (and modernized) those hooks.
  4032. ;; So the format conversion should be all that is needed.
  4033.  
  4034. (defun ange-ftp-insert-directory (file switches &optional wildcard full)
  4035.   (let ((short (ange-ftp-abbreviate-filename file))
  4036.     (parsed (ange-ftp-ftp-name (expand-file-name file))))
  4037.     (if parsed
  4038.     (insert
  4039.      (if wildcard
  4040.          (let ((default-directory (file-name-directory file)))
  4041.            (ange-ftp-ls (file-name-nondirectory file) switches nil nil t))
  4042.        (ange-ftp-ls file switches full)))
  4043.       (ange-ftp-real-insert-directory file switches wildcard full))))
  4044.  
  4045. (defun ange-ftp-dired-uncache (dir)
  4046.   (if (ange-ftp-ftp-name (expand-file-name dir))
  4047.       (setq ange-ftp-ls-cache-file nil)))
  4048.  
  4049. (defvar ange-ftp-sans-version-alist nil
  4050.   "Alist of mapping host type into function to remove file version numbers.")
  4051.  
  4052. (defun ange-ftp-file-name-sans-versions (file keep-backup-version)
  4053.   (setq file (ange-ftp-abbreviate-filename file))
  4054.   (let ((parsed (ange-ftp-ftp-name file))
  4055.     host-type func)
  4056.     (if parsed
  4057.     (setq host-type (ange-ftp-host-type (car parsed))
  4058.           func (cdr (assq (ange-ftp-host-type (car parsed))
  4059.                   ange-ftp-sans-version-alist))))
  4060.     (if func (funcall func file keep-backup-version)
  4061.       (ange-ftp-real-file-name-sans-versions file keep-backup-version))))
  4062.  
  4063. ;;; This doesn't work yet; a new hook needs to be created.
  4064. ;;; Maybe the new hook should be in call-process.
  4065. (defun ange-ftp-shell-command (command)
  4066.   (let* ((parsed (ange-ftp-ftp-name default-directory))
  4067.      (host (nth 0 parsed))
  4068.      (user (nth 1 parsed))
  4069.      (name (nth 2 parsed)))
  4070.     (if (not parsed)
  4071.     (ange-ftp-real-shell-command command)
  4072.       (if (> (length name) 0)        ; else it's $HOME
  4073.       (setq command (concat "cd " name "; " command)))
  4074.       (setq command
  4075.         (format  "%s %s \"%s\""    ; remsh -l USER does not work well
  4076.                     ; on a hp-ux machine I tried
  4077.              remote-shell-program host command))
  4078.       (ange-ftp-message "Remote command '%s' ..." command)
  4079.       ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
  4080.       ;; would prepend "cd default-directory" --- which bombs because
  4081.       ;; default-directory is in ange-ftp syntax for remote file names.
  4082.       (ange-ftp-real-shell-command command))))
  4083.  
  4084. ;;; This is the handler for call-process.
  4085. (defun ange-ftp-dired-call-process (program discard &rest arguments)
  4086.   ;; PROGRAM is always one of those below in the cond in dired.el.
  4087.   ;; The ARGUMENTS are (nearly) always files.
  4088.   (if (ange-ftp-ftp-name default-directory)
  4089.       ;; Can't use ange-ftp-dired-host-type here because the current
  4090.       ;; buffer is *dired-check-process output*
  4091.       (condition-case oops
  4092.       (cond ((equal "chmod" program)
  4093.          (ange-ftp-call-chmod arguments))
  4094.         ;; ((equal "chgrp" program))
  4095.         ;; ((equal dired-chown-program program))
  4096.         (t (error "Unknown remote command: %s" program)))
  4097.     (ftp-error (insert (format "%s: %s, %s\n"
  4098.                     (nth 1 oops)
  4099.                     (nth 2 oops)
  4100.                     (nth 3 oops)))
  4101.            ;; Caller expects nonzero value to mean failure.
  4102.            1)
  4103.     (error (insert (format "%s\n" (nth 1 oops)))
  4104.            1))
  4105.     (apply 'call-process program nil (not discard) nil arguments)))
  4106.  
  4107. (defvar ange-ftp-remote-shell "rsh" 
  4108.   "Remote shell to use for chmod, if FTP server rejects the `chmod' command.")
  4109.  
  4110. ;; Handle an attempt to run chmod on a remote file
  4111. ;; by using the ftp chmod command.
  4112. (defun ange-ftp-call-chmod (args)
  4113.   (if (< (length args) 2)
  4114.       (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
  4115.   (let ((mode (car args)))
  4116.     (mapcar
  4117.      (function
  4118.       (lambda (file)
  4119.     (setq file (expand-file-name file))
  4120.     (let ((parsed (ange-ftp-ftp-name file)))
  4121.       (if parsed
  4122.           (let* ((host (nth 0 parsed))
  4123.              (user (nth 1 parsed))
  4124.              (name (ange-ftp-quote-string (nth 2 parsed)))
  4125.              (abbr (ange-ftp-abbreviate-filename file))
  4126.              (result (ange-ftp-send-cmd host user
  4127.                         (list 'chmod mode name)
  4128.                         (format "doing chmod %s"
  4129.                             abbr))))
  4130.         (or (car result)
  4131.             (call-process 
  4132.              ange-ftp-remote-shell
  4133.              nil t nil host "chmod" mode name)))))))
  4134.      (cdr args)))
  4135.   (setq ange-ftp-ls-cache-file nil)    ;Stop confusing Dired.
  4136.   0)
  4137.  
  4138. ;;; This is turned off because it has nothing properly to do
  4139. ;;; with dired.  It could be reasonable to adapt this to
  4140. ;;; replace ange-ftp-copy-file.
  4141.  
  4142. ;;;;; ------------------------------------------------------------
  4143. ;;;;; Noddy support for async copy-file within dired.
  4144. ;;;;; ------------------------------------------------------------
  4145.  
  4146. ;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
  4147. ;;  "Documented as original."
  4148. ;;  (dired-handle-overwrite to)
  4149. ;;  (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
  4150. ;;                   cont nowait))
  4151.  
  4152. ;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
  4153. ;;                         &optional marker-char op1
  4154. ;;                         how-to)
  4155. ;;  "Documented as original."
  4156. ;;  ;; we need to let ange-ftp-dired-create-files know that we indirectly
  4157. ;;  ;; called it rather than somebody else.
  4158. ;;  (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
  4159. ;;    (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
  4160. ;;                     arg marker-char op1 how-to)))
  4161.  
  4162. ;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
  4163. ;;                         &optional marker-char)
  4164. ;;  "Documented as original."
  4165. ;;  (if (and (boundp 'ange-ftp-dired-do-create-files)
  4166. ;;       ;; called from ange-ftp-dired-do-create-files?
  4167. ;;       ange-ftp-dired-do-create-files
  4168. ;;       ;; any files worth copying?
  4169. ;;       fn-list
  4170. ;;       ;; we only support async copy-file at the mo.
  4171. ;;       (eq file-creator 'dired-copy-file)
  4172. ;;       ;; it is only worth calling the alternative function for remote files
  4173. ;;       ;; as we tie ourself in recursive knots otherwise.
  4174. ;;       (or (ange-ftp-ftp-name (car fn-list))
  4175. ;;           ;; we can only call the name constructor for dired-do-create-files
  4176. ;;           ;; since the one for regexps starts prompting here, there and
  4177. ;;           ;; everywhere.
  4178. ;;           (ange-ftp-ftp-name (funcall name-constructor (car fn-list)))))
  4179. ;;      ;; use the process-filter driven routine rather than the iterative one.
  4180. ;;      (ange-ftp-dcf-1 file-creator
  4181. ;;                 operation
  4182. ;;               fn-list
  4183. ;;               name-constructor
  4184. ;;               (and (boundp 'target) target)    ;dynamically bound
  4185. ;;               marker-char
  4186. ;;               (current-buffer)
  4187. ;;               nil    ;overwrite-query
  4188. ;;               nil    ;overwrite-backup-query
  4189. ;;               nil    ;failures
  4190. ;;               nil    ;skipped
  4191. ;;               0        ;success-count
  4192. ;;               (length fn-list) ;total
  4193. ;;               )
  4194. ;;    ;; normal case... use the interactive routine... much cheaper.
  4195. ;;    (ange-ftp-real-dired-create-files file-creator operation fn-list
  4196. ;;                      name-constructor marker-char)))
  4197.  
  4198. ;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
  4199. ;;               target marker-char buffer overwrite-query 
  4200. ;;               overwrite-backup-query failures skipped
  4201. ;;               success-count total)
  4202. ;;  (let ((old-buf (current-buffer)))
  4203. ;;    (unwind-protect
  4204. ;;    (progn
  4205. ;;      (set-buffer buffer)
  4206. ;;      (if (null fn-list)
  4207. ;;          (ange-ftp-dcf-3 failures operation total skipped
  4208. ;;                  success-count buffer)
  4209.         
  4210. ;;        (let* ((from (car fn-list))
  4211. ;;           (to (funcall name-constructor from)))
  4212. ;;          (if (equal to from)
  4213. ;;          (progn
  4214. ;;            (setq to nil)
  4215. ;;            (dired-log "Cannot %s to same file: %s\n"
  4216. ;;                   (downcase operation) from)))
  4217. ;;          (if (not to)
  4218. ;;          (ange-ftp-dcf-1 file-creator
  4219. ;;                  operation
  4220. ;;                  (cdr fn-list)
  4221. ;;                  name-constructor
  4222. ;;                  target
  4223. ;;                  marker-char
  4224. ;;                  buffer
  4225. ;;                  overwrite-query
  4226. ;;                  overwrite-backup-query
  4227. ;;                  failures
  4228. ;;                  (cons (dired-make-relative from) skipped)
  4229. ;;                  success-count
  4230. ;;                  total)
  4231. ;;        (let* ((overwrite (file-exists-p to))
  4232. ;;               (overwrite-confirmed    ; for dired-handle-overwrite
  4233. ;;            (and overwrite
  4234. ;;                 (let ((help-form '(format "\
  4235. ;;Type SPC or `y' to overwrite file `%s',
  4236. ;;DEL or `n' to skip to next,
  4237. ;;ESC or `q' to not overwrite any of the remaining files,
  4238. ;;`!' to overwrite all remaining files with no more questions." to)))
  4239. ;;                   (dired-query 'overwrite-query
  4240. ;;                        "Overwrite `%s'?" to))))
  4241. ;;               ;; must determine if FROM is marked before file-creator
  4242. ;;               ;; gets a chance to delete it (in case of a move).
  4243. ;;               (actual-marker-char
  4244. ;;            (cond  ((integerp marker-char) marker-char)
  4245. ;;                   (marker-char (dired-file-marker from)) ; slow
  4246. ;;                   (t nil))))
  4247. ;;          (condition-case err
  4248. ;;              (funcall file-creator from to overwrite-confirmed
  4249. ;;                   (list (function ange-ftp-dcf-2)
  4250. ;;                     nil    ;err
  4251. ;;                     file-creator operation fn-list
  4252. ;;                     name-constructor
  4253. ;;                     target
  4254. ;;                     marker-char actual-marker-char
  4255. ;;                     buffer to from
  4256. ;;                     overwrite
  4257. ;;                     overwrite-confirmed
  4258. ;;                     overwrite-query 
  4259. ;;                     overwrite-backup-query
  4260. ;;                     failures skipped success-count
  4261. ;;                     total)
  4262. ;;                   t)
  4263. ;;            (file-error        ; FILE-CREATOR aborted
  4264. ;;             (ange-ftp-dcf-2 nil ;result
  4265. ;;                     nil ;line
  4266. ;;                     err
  4267. ;;                     file-creator operation fn-list
  4268. ;;                     name-constructor
  4269. ;;                     target
  4270. ;;                     marker-char actual-marker-char
  4271. ;;                     buffer to from
  4272. ;;                     overwrite
  4273. ;;                     overwrite-confirmed
  4274. ;;                     overwrite-query 
  4275. ;;                     overwrite-backup-query
  4276. ;;                     failures skipped success-count
  4277. ;;                     total))))))))
  4278. ;;      (set-buffer old-buf))))
  4279.  
  4280. ;;(defun ange-ftp-dcf-2 (result line err
  4281. ;;                  file-creator operation fn-list
  4282. ;;                  name-constructor
  4283. ;;                  target
  4284. ;;                  marker-char actual-marker-char
  4285. ;;                  buffer to from
  4286. ;;                  overwrite
  4287. ;;                  overwrite-confirmed
  4288. ;;                  overwrite-query 
  4289. ;;                  overwrite-backup-query
  4290. ;;                  failures skipped success-count
  4291. ;;                  total)
  4292. ;;  (let ((old-buf (current-buffer)))
  4293. ;;    (unwind-protect
  4294. ;;    (progn
  4295. ;;      (set-buffer buffer)
  4296. ;;      (if (or err (not result))
  4297. ;;          (progn
  4298. ;;        (setq failures (cons (dired-make-relative from) failures))
  4299. ;;        (dired-log "%s `%s' to `%s' failed:\n%s\n"
  4300. ;;               operation from to (or err line)))
  4301. ;;        (if overwrite
  4302. ;;        ;; If we get here, file-creator hasn't been aborted
  4303. ;;        ;; and the old entry (if any) has to be deleted
  4304. ;;        ;; before adding the new entry.
  4305. ;;        (dired-remove-file to))
  4306. ;;        (setq success-count (1+ success-count))
  4307. ;;        (message "%s: %d of %d" operation success-count total)
  4308. ;;        (dired-add-file to actual-marker-char))
  4309.       
  4310. ;;      (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
  4311. ;;              name-constructor
  4312. ;;              target
  4313. ;;              marker-char
  4314. ;;              buffer
  4315. ;;              overwrite-query 
  4316. ;;              overwrite-backup-query
  4317. ;;              failures skipped success-count
  4318. ;;              total))
  4319. ;;      (set-buffer old-buf))))
  4320.  
  4321. ;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count
  4322. ;;                buffer)
  4323. ;;  (let ((old-buf (current-buffer)))
  4324. ;;    (unwind-protect
  4325. ;;    (progn
  4326. ;;      (set-buffer buffer)
  4327. ;;      (cond
  4328. ;;       (failures
  4329. ;;        (dired-log-summary
  4330. ;;         (message "%s failed for %d of %d file%s %s"
  4331. ;;              operation (length failures) total
  4332. ;;              (dired-plural-s total) failures)))
  4333. ;;       (skipped
  4334. ;;        (dired-log-summary
  4335. ;;         (message "%s: %d of %d file%s skipped %s"
  4336. ;;              operation (length skipped) total
  4337. ;;              (dired-plural-s total) skipped)))
  4338. ;;       (t
  4339. ;;        (message "%s: %s file%s."
  4340. ;;             operation success-count (dired-plural-s success-count))))
  4341. ;;      (dired-move-to-filename))
  4342. ;;      (set-buffer old-buf))))
  4343.  
  4344. ;;;; -----------------------------------------------
  4345. ;;;; Unix Descriptive Listing (dl) Support
  4346. ;;;; -----------------------------------------------
  4347.  
  4348. ;; This is turned off because nothing uses it currently
  4349. ;; and because I don't understand what it's supposed to be for. --rms.
  4350.  
  4351. ;;(defconst ange-ftp-dired-dl-re-dir
  4352. ;;  "^. [^ /]+/[ \n]"
  4353. ;;  "Regular expression to use to search for dl directories.")
  4354.  
  4355. ;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
  4356. ;;    (setq ange-ftp-dired-re-dir-alist
  4357. ;;      (cons (cons 'unix:dl  ange-ftp-dired-dl-re-dir)
  4358. ;;        ange-ftp-dired-re-dir-alist)))
  4359.  
  4360. ;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
  4361. ;;  "In dired, move to the first character of the filename on this line."
  4362. ;;  ;; This is the Unix dl version.
  4363. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  4364. ;;  (let (case-fold-search)
  4365. ;;    (beginning-of-line)
  4366. ;;    (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
  4367. ;;    (goto-char (+ (point) 2))
  4368. ;;      (if raise-error
  4369. ;;      (error "No file on this line")
  4370. ;;    nil))))
  4371.  
  4372. ;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
  4373. ;;    (setq ange-ftp-dired-move-to-filename-alist
  4374. ;;      (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
  4375. ;;        ange-ftp-dired-move-to-filename-alist)))
  4376.  
  4377. ;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
  4378. ;;  ;; Assumes point is at beginning of filename.
  4379. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  4380. ;;  ;; On failure, signals an error or returns nil.
  4381. ;;  ;; This is the Unix dl version.
  4382. ;;  (let ((opoint (point))
  4383. ;;    case-fold-search hidden)
  4384. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  4385. ;;    (setq hidden (and selective-display
  4386. ;;               (save-excursion
  4387. ;;             (search-forward "\r" eol t))))
  4388. ;;    (if hidden
  4389. ;;    (if no-error
  4390. ;;        nil
  4391. ;;      (error
  4392. ;;       (substitute-command-keys
  4393. ;;        "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  4394. ;;      (skip-chars-forward "^ /" eol)
  4395. ;;      (if (eq opoint (point))
  4396. ;;      (if no-error
  4397. ;;          nil
  4398. ;;        (error "No file on this line"))
  4399. ;;    (point)))))
  4400.  
  4401. ;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
  4402. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  4403. ;;      (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
  4404. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  4405.  
  4406. ;;;; ------------------------------------------------------------
  4407. ;;;; VOS support (VOS support is probably broken,
  4408. ;;;; but I don't know anything about VOS.)
  4409. ;;;; ------------------------------------------------------------
  4410. ;
  4411. ;(defun ange-ftp-fix-name-for-vos (name &optional reverse)
  4412. ;  (setq name (copy-sequence name))
  4413. ;  (let ((from (if reverse ?\> ?\/))
  4414. ;    (to (if reverse ?\/ ?\>))
  4415. ;    (i (1- (length name))))
  4416. ;    (while (>= i 0)
  4417. ;      (if (= (aref name i) from)
  4418. ;      (aset name i to))
  4419. ;      (setq i (1- i)))
  4420. ;    name))
  4421. ;
  4422. ;(or (assq 'vos ange-ftp-fix-name-func-alist)
  4423. ;    (setq ange-ftp-fix-name-func-alist
  4424. ;      (cons '(vos . ange-ftp-fix-name-for-vos)
  4425. ;        ange-ftp-fix-name-func-alist)))
  4426. ;
  4427. ;(or (memq 'vos ange-ftp-dumb-host-types)
  4428. ;    (setq ange-ftp-dumb-host-types
  4429. ;      (cons 'vos ange-ftp-dumb-host-types)))
  4430. ;
  4431. ;(defun ange-ftp-fix-dir-name-for-vos (dir-name)
  4432. ;  (ange-ftp-fix-name-for-vos
  4433. ;   (concat dir-name
  4434. ;       (if (eq ?/ (aref dir-name (1- (length dir-name))))
  4435. ;           "" "/")
  4436. ;       "*")))
  4437. ;
  4438. ;(or (assq 'vos ange-ftp-fix-dir-name-func-alist)
  4439. ;    (setq ange-ftp-fix-dir-name-func-alist
  4440. ;      (cons '(vos . ange-ftp-fix-dir-name-for-vos)
  4441. ;        ange-ftp-fix-dir-name-func-alist)))
  4442. ;
  4443. ;(defvar ange-ftp-vos-host-regexp nil
  4444. ;  "If a host matches this regexp then it is assumed to be running VOS.")
  4445. ;
  4446. ;(defun ange-ftp-vos-host (host)
  4447. ;  (and ange-ftp-vos-host-regexp
  4448. ;       (save-match-data
  4449. ;     (string-match ange-ftp-vos-host-regexp host))))
  4450. ;
  4451. ;(defun ange-ftp-parse-vos-listing ()
  4452. ;  "Parse the current buffer which is assumed to be in VOS list -all
  4453. ;format, and return a hashtable as the result."
  4454. ;  (let ((tbl (ange-ftp-make-hashtable))
  4455. ;    (type-list
  4456. ;     '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
  4457. ;       ("^Dirs: [0-9]+\n+" t 30)))
  4458. ;    type-regexp type-is-dir type-col file)
  4459. ;    (goto-char (point-min))
  4460. ;    (save-match-data
  4461. ;      (while type-list
  4462. ;    (setq type-regexp (car (car type-list))
  4463. ;          type-is-dir (nth 1 (car type-list))
  4464. ;          type-col (nth 2 (car type-list))
  4465. ;          type-list (cdr type-list))
  4466. ;    (if (re-search-forward type-regexp nil t)
  4467. ;        (while (eq (char-after (point)) ? )
  4468. ;          (move-to-column type-col)
  4469. ;          (setq file (buffer-substring (point)
  4470. ;                       (progn
  4471. ;                         (end-of-line 1)
  4472. ;                         (point))))
  4473. ;          (ange-ftp-put-hash-entry file type-is-dir tbl)
  4474. ;          (forward-line 1))))
  4475. ;      (ange-ftp-put-hash-entry "." 'vosdir tbl)
  4476. ;      (ange-ftp-put-hash-entry ".." 'vosdir tbl))
  4477. ;    tbl))
  4478. ;
  4479. ;(or (assq 'vos ange-ftp-parse-list-func-alist)
  4480. ;    (setq ange-ftp-parse-list-func-alist
  4481. ;      (cons '(vos . ange-ftp-parse-vos-listing)
  4482. ;        ange-ftp-parse-list-func-alist)))
  4483.  
  4484. ;;;; ------------------------------------------------------------
  4485. ;;;; VMS support.
  4486. ;;;; ------------------------------------------------------------
  4487.  
  4488. ;; Convert NAME from UNIX-ish to VMS.  If REVERSE given then convert from VMS
  4489. ;; to UNIX-ish.
  4490. (defun ange-ftp-fix-name-for-vms (name &optional reverse)
  4491.   (save-match-data
  4492.     (if reverse
  4493.     (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name)
  4494.         (let (drive dir file)
  4495.           (if (match-beginning 1)
  4496.           (setq drive (substring name
  4497.                      (match-beginning 1)
  4498.                      (match-end 1))))
  4499.           (if (match-beginning 2)
  4500.           (setq dir
  4501.             (substring name (match-beginning 2) (match-end 2))))
  4502.           (if (match-beginning 3)
  4503.           (setq file
  4504.             (substring name (match-beginning 3) (match-end 3))))
  4505.           (and dir
  4506.            (setq dir (apply (function concat)
  4507.                     (mapcar (function
  4508.                          (lambda (char)
  4509.                            (if (= char ?.)
  4510.                            (vector ?/)
  4511.                          (vector char))))
  4512.                         (substring dir 1 -1)))))
  4513.           (concat (and drive
  4514.                (concat "/" drive "/"))
  4515.               dir (and dir "/")
  4516.               file))
  4517.       (error "name %s didn't match" name))
  4518.       (let (drive dir file tmp)
  4519.     (if (string-match "^/[^:]+:/" name)
  4520.         (setq drive (substring name 1
  4521.                    (1- (match-end 0)))
  4522.           name (substring name (match-end 0))))
  4523.     (setq tmp (file-name-directory name))
  4524.     (if tmp
  4525.         (setq dir (apply (function concat)
  4526.                  (mapcar (function
  4527.                       (lambda (char)
  4528.                     (if (= char ?/)
  4529.                         (vector ?.)
  4530.                       (vector char))))
  4531.                      (substring tmp 0 -1)))))
  4532.     (setq file (file-name-nondirectory name))
  4533.     (concat drive
  4534.         (and dir (concat "[" (if drive nil ".") dir "]"))
  4535.         file)))))
  4536.  
  4537. ;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
  4538. ;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
  4539.  
  4540. (or (assq 'vms ange-ftp-fix-name-func-alist)
  4541.     (setq ange-ftp-fix-name-func-alist
  4542.       (cons '(vms . ange-ftp-fix-name-for-vms)
  4543.         ange-ftp-fix-name-func-alist)))
  4544.  
  4545. (or (memq 'vms ange-ftp-dumb-host-types)
  4546.     (setq ange-ftp-dumb-host-types
  4547.       (cons 'vms ange-ftp-dumb-host-types)))
  4548.  
  4549. ;; It is important that this function barf for directories for which we know
  4550. ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
  4551. ;; This is because it saves an unnecessary FTP error, or possibly the listing
  4552. ;; might succeed, but give erroneous info. This last case is particularly
  4553. ;; likely for OS's (like MTS) for which we need to use a wildcard in order
  4554. ;; to list a directory.
  4555.  
  4556. ;; Convert name from UNIX-ish to VMS ready for a DIRectory listing.
  4557. (defun ange-ftp-fix-dir-name-for-vms (dir-name)
  4558.   ;; Should there be entries for .. -> [-] and . -> [] below. Don't
  4559.   ;; think so, because expand-filename should have already short-circuited
  4560.   ;; them.
  4561.   (cond ((string-equal dir-name "/")
  4562.      (error "Cannot get listing for fictitious \"/\" directory."))
  4563.     ((string-match "^/[-A-Z0-9_$]+:/$" dir-name)
  4564.      (error "Cannot get listing for device."))
  4565.     ((ange-ftp-fix-name-for-vms dir-name))))
  4566.   
  4567. (or (assq 'vms ange-ftp-fix-dir-name-func-alist)
  4568.     (setq ange-ftp-fix-dir-name-func-alist
  4569.       (cons '(vms . ange-ftp-fix-dir-name-for-vms)
  4570.         ange-ftp-fix-dir-name-func-alist)))
  4571.  
  4572. (defvar ange-ftp-vms-host-regexp nil)
  4573.  
  4574. ;; Return non-nil if HOST is running VMS.
  4575. (defun ange-ftp-vms-host (host)
  4576.   (and ange-ftp-vms-host-regexp
  4577.        (save-match-data
  4578.      (string-match ange-ftp-vms-host-regexp host))))
  4579.  
  4580. ;; Because some VMS ftp servers convert filenames to lower case
  4581. ;; we allow a-z in the filename regexp. I'm not too happy about this.
  4582.  
  4583. (defconst ange-ftp-vms-filename-regexp
  4584.   (concat
  4585.    "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\."
  4586.    "[-_A-Za-z0-9$]*;+[0-9]*\\)")
  4587.   "Regular expression to match for a valid VMS file name in Dired buffer.
  4588. Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
  4589. Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
  4590. Other orders of $ and _ seem to all work just fine.")
  4591.  
  4592. ;; These parsing functions are as general as possible because the syntax
  4593. ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
  4594. ;; the VMS filename syntax is so rigid. If they bomb on a listing in the
  4595. ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
  4596. ;; from vms.weird.net, then too bad.
  4597.  
  4598. ;; Extract the next filename from a VMS dired-like listing.
  4599. (defun ange-ftp-parse-vms-filename ()
  4600.   (if (re-search-forward
  4601.      ange-ftp-vms-filename-regexp
  4602.      nil t)
  4603.     (buffer-substring (match-beginning 0) (match-end 0))))
  4604.  
  4605. ;; Parse the current buffer which is assumed to be in MultiNet FTP dir
  4606. ;; format, and return a hashtable as the result.
  4607. (defun ange-ftp-parse-vms-listing ()
  4608.   (let ((tbl (ange-ftp-make-hashtable))
  4609.     file)
  4610.     (goto-char (point-min))
  4611.     (save-match-data
  4612.       (while (setq file (ange-ftp-parse-vms-filename))
  4613.     (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
  4614.         ;; deal with directories
  4615.         (ange-ftp-put-hash-entry
  4616.          (substring file 0 (match-beginning 0)) t tbl)
  4617.       (ange-ftp-put-hash-entry file nil tbl)
  4618.       (if (string-match ";[0-9]+$" file) ; deal with extension
  4619.           ;; sans extension
  4620.           (ange-ftp-put-hash-entry
  4621.            (substring file 0 (match-beginning 0)) nil tbl)))
  4622.     (forward-line 1))
  4623.       ;; Would like to look for a "Total" line, or a "Directory" line to
  4624.       ;; make sure that the listing isn't complete garbage before putting
  4625.       ;; in "." and "..", but we can't even count on all VAX's giving us
  4626.       ;; either of these.
  4627.        (ange-ftp-put-hash-entry "." t tbl)
  4628.        (ange-ftp-put-hash-entry ".." t tbl))
  4629.     tbl))
  4630.  
  4631. (or (assq 'vms ange-ftp-parse-list-func-alist)
  4632.     (setq ange-ftp-parse-list-func-alist
  4633.       (cons '(vms . ange-ftp-parse-vms-listing)
  4634.         ange-ftp-parse-list-func-alist)))
  4635.  
  4636. ;; This version only deletes file entries which have
  4637. ;; explicit version numbers, because that is all VMS allows.
  4638.  
  4639. ;; Can the following two functions be speeded up using file
  4640. ;; completion functions?
  4641.  
  4642. (defun ange-ftp-vms-delete-file-entry (name &optional dir-p)
  4643.   (if dir-p
  4644.       (ange-ftp-internal-delete-file-entry name t)
  4645.     (save-match-data
  4646.       (let ((file (ange-ftp-get-file-part name)))
  4647.     (if (string-match ";[0-9]+$" file)
  4648.         ;; In VMS you can't delete a file without an explicit    
  4649.         ;; version number, or wild-card (e.g. FOO;*)
  4650.         ;; For now, we give up on wildcards.
  4651.         (let ((files (ange-ftp-get-hash-entry
  4652.               (file-name-directory name)
  4653.               ange-ftp-files-hashtable)))
  4654.           (if files
  4655.           (let* ((root (substring file 0
  4656.                       (match-beginning 0)))
  4657.              (regexp (concat "^"
  4658.                      (regexp-quote root)
  4659.                      ";[0-9]+$"))
  4660.              versions)
  4661.             (ange-ftp-del-hash-entry file files)
  4662.             ;; Now we need to check if there are any
  4663.             ;; versions left. If not, then delete the
  4664.             ;; root entry.
  4665.             (mapatoms
  4666.              '(lambda (sym)
  4667.             (and (string-match regexp (get sym 'key))
  4668.                  (setq versions t)))
  4669.              files)
  4670.             (or versions
  4671.             (ange-ftp-del-hash-entry root files))))))))))
  4672.  
  4673. (or (assq 'vms ange-ftp-delete-file-entry-alist)
  4674.     (setq ange-ftp-delete-file-entry-alist
  4675.       (cons '(vms . ange-ftp-vms-delete-file-entry)
  4676.         ange-ftp-delete-file-entry-alist)))
  4677.  
  4678. (defun ange-ftp-vms-add-file-entry (name &optional dir-p)
  4679.   (if dir-p
  4680.       (ange-ftp-internal-add-file-entry name t)
  4681.     (let ((files (ange-ftp-get-hash-entry
  4682.           (file-name-directory name)
  4683.           ange-ftp-files-hashtable)))
  4684.       (if files
  4685.       (let ((file (ange-ftp-get-file-part name)))
  4686.         (save-match-data
  4687.           (if (string-match ";[0-9]+$" file)
  4688.           (ange-ftp-put-hash-entry
  4689.            (substring file 0 (match-beginning 0))
  4690.            nil files)
  4691.         ;; Need to figure out what version of the file
  4692.         ;; is being added.
  4693.         (let ((regexp (concat "^"
  4694.                       (regexp-quote file)
  4695.                       ";\\([0-9]+\\)$"))
  4696.               (version 0))
  4697.           (mapatoms
  4698.            '(lambda (sym)
  4699.               (let ((name (get sym 'key)))
  4700.             (and (string-match regexp name)
  4701.                  (setq version
  4702.                    (max version
  4703.                     (string-to-int
  4704.                      (substring name
  4705.                             (match-beginning 1)
  4706.                             (match-end 1))))))))
  4707.            files)
  4708.           (setq version (1+ version))
  4709.           (ange-ftp-put-hash-entry
  4710.            (concat file ";" (int-to-string version))
  4711.            nil files))))
  4712.         (ange-ftp-put-hash-entry file nil files))))))
  4713.  
  4714. (or (assq 'vms ange-ftp-add-file-entry-alist)
  4715.     (setq ange-ftp-add-file-entry-alist
  4716.       (cons '(vms . ange-ftp-vms-add-file-entry)
  4717.         ange-ftp-add-file-entry-alist)))
  4718.  
  4719.  
  4720. (defun ange-ftp-add-vms-host (host)
  4721.   "Mark HOST as the name of a machine running VMS."
  4722.   (interactive
  4723.    (list (read-string "Host: "
  4724.               (let ((name (or (buffer-file-name) default-directory)))
  4725.             (and name (car (ange-ftp-ftp-name name)))))))
  4726.   (if (not (ange-ftp-vms-host host))
  4727.       (setq ange-ftp-vms-host-regexp
  4728.         (concat "^" (regexp-quote host) "$"
  4729.             (and ange-ftp-vms-host-regexp "\\|")
  4730.             ange-ftp-vms-host-regexp)
  4731.         ange-ftp-host-cache nil)))
  4732.  
  4733.  
  4734. (defun ange-ftp-vms-file-name-as-directory (name)
  4735.   (save-match-data
  4736.     (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
  4737.     (setq name (substring name 0 (match-beginning 0))))
  4738.     (ange-ftp-real-file-name-as-directory name)))
  4739.  
  4740. (or (assq 'vms ange-ftp-file-name-as-directory-alist)
  4741.     (setq ange-ftp-file-name-as-directory-alist
  4742.       (cons '(vms . ange-ftp-vms-file-name-as-directory)
  4743.         ange-ftp-file-name-as-directory-alist)))
  4744.  
  4745. ;;; Tree dired support:
  4746.  
  4747. ;; For this code I have borrowed liberally from Sebastian Kremer's
  4748. ;; dired-vms.el
  4749.  
  4750.  
  4751. ;;;; These regexps must be anchored to beginning of line.
  4752. ;;;; Beware that the ftpd may put the device in front of the filename.
  4753.  
  4754. ;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
  4755. ;;  "Regular expression to use to search for VMS executable files.")
  4756.  
  4757. ;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
  4758. ;;  "Regular expression to use to search for VMS directories.")
  4759.  
  4760. ;;(or (assq 'vms ange-ftp-dired-re-exe-alist)
  4761. ;;    (setq ange-ftp-dired-re-exe-alist
  4762. ;;      (cons (cons 'vms  ange-ftp-dired-vms-re-exe)
  4763. ;;        ange-ftp-dired-re-exe-alist)))
  4764.  
  4765. ;;(or (assq 'vms ange-ftp-dired-re-dir-alist)
  4766. ;;    (setq ange-ftp-dired-re-dir-alist
  4767. ;;      (cons (cons 'vms  ange-ftp-dired-vms-re-dir)
  4768. ;;        ange-ftp-dired-re-dir-alist)))
  4769.  
  4770. ;;(defun ange-ftp-dired-vms-insert-headerline (dir)
  4771. ;;  ;; VMS inserts a headerline. I would prefer the headerline
  4772. ;;  ;; to be in ange-ftp format. This version tries to
  4773. ;;  ;; be careful, because we can't count on a headerline
  4774. ;;  ;; over ftp, and we wouldn't want to delete anything
  4775. ;;  ;; important.
  4776. ;;  (save-excursion
  4777. ;;    (if (looking-at "^  wildcard ")
  4778. ;;    (forward-line 1))
  4779. ;;    (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
  4780. ;;    (delete-region (point) (match-end 0))))
  4781. ;;  (ange-ftp-real-dired-insert-headerline dir))
  4782.  
  4783. ;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist)
  4784. ;;    (setq ange-ftp-dired-insert-headerline-alist
  4785. ;;      (cons '(vms . ange-ftp-dired-vms-insert-headerline)
  4786. ;;        ange-ftp-dired-insert-headerline-alist)))
  4787.  
  4788. ;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
  4789. ;;  "In dired, move to first char of filename on this line.
  4790. ;;Returns position (point) or nil if no filename on this line."
  4791. ;;  ;; This is the VMS version.
  4792. ;;  (let (case-fold-search)
  4793. ;;    (or eol (setq eol (progn (end-of-line) (point))))
  4794. ;;    (beginning-of-line)
  4795. ;;    (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
  4796. ;;    (goto-char (match-beginning 1))
  4797. ;;      (if raise-error
  4798. ;;      (error "No file on this line")
  4799. ;;    nil))))
  4800.  
  4801. ;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist)
  4802. ;;    (setq ange-ftp-dired-move-to-filename-alist
  4803. ;;      (cons '(vms . ange-ftp-dired-vms-move-to-filename)
  4804. ;;        ange-ftp-dired-move-to-filename-alist)))
  4805.  
  4806. ;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
  4807. ;;  ;; Assumes point is at beginning of filename.
  4808. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  4809. ;;  ;; case-fold-search must be nil, at least for VMS.
  4810. ;;  ;; On failure, signals an error or returns nil.
  4811. ;;  ;; This is the VMS version.
  4812. ;;  (let (opoint hidden case-fold-search)
  4813. ;;    (setq opoint (point))
  4814. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  4815. ;;    (setq hidden (and selective-display
  4816. ;;              (save-excursion (search-forward "\r" eol t))))
  4817. ;;    (if hidden
  4818. ;;    nil
  4819. ;;      (re-search-forward ange-ftp-vms-filename-regexp eol t))
  4820. ;;    (or no-error
  4821. ;;    (not (eq opoint (point)))
  4822. ;;    (error
  4823. ;;     (if hidden
  4824. ;;         (substitute-command-keys
  4825. ;;          "File line is hidden, type \\[dired-hide-subdir] to unhide")
  4826. ;;       "No file on this line")))
  4827. ;;    (if (eq opoint (point))
  4828. ;;    nil
  4829. ;;      (point))))
  4830.  
  4831. ;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
  4832. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  4833. ;;      (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
  4834. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  4835.  
  4836. ;;(defun ange-ftp-dired-vms-between-files ()
  4837. ;;  (save-excursion
  4838. ;;    (beginning-of-line)
  4839. ;;    (or (equal (following-char) 10) ; newline
  4840. ;;     (equal (following-char) 9)     ; tab
  4841. ;;     (progn (forward-char 2)
  4842. ;;        (or (looking-at "Total of")
  4843. ;;        (equal (following-char) 32))))))
  4844.  
  4845. ;;(or (assq 'vms ange-ftp-dired-between-files-alist)
  4846. ;;    (setq ange-ftp-dired-between-files-alist
  4847. ;;      (cons '(vms . ange-ftp-dired-vms-between-files)
  4848. ;;        ange-ftp-dired-between-files-alist)))
  4849.  
  4850. ;; Beware! In VMS filenames must be of the form "FILE.TYPE".
  4851. ;; Therefore, we cannot just append a ".Z" to filenames for
  4852. ;; compressed files. Instead, we turn "FILE.TYPE" into
  4853. ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
  4854.  
  4855. (defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
  4856.   (cond
  4857.    ((string-match "-Z;[0-9]+$" name)
  4858.     (list nil (substring name 0 (match-beginning 0))))
  4859.    ((string-match ";[0-9]+$" name)
  4860.     (list nil (substring name 0 (match-beginning 0))))
  4861.    ((string-match "-Z$" name)
  4862.     (list nil (substring name 0 -2)))
  4863.    (t
  4864.     (list t
  4865.       (if (string-match ";[0-9]+$" name)
  4866.           (concat (substring name 0 (match-beginning 0))
  4867.               "-Z")
  4868.         (concat name "-Z"))))))
  4869.  
  4870. (or (assq 'vms ange-ftp-make-compressed-filename-alist)
  4871.     (setq ange-ftp-make-compressed-filename-alist
  4872.       (cons '(vms . ange-ftp-vms-make-compressed-filename)
  4873.         ange-ftp-make-compressed-filename-alist)))
  4874.  
  4875. ;;;; When the filename is too long, VMS will use two lines to list a file
  4876. ;;;; (damn them!) This will confuse dired. To solve this, need to convince
  4877. ;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
  4878. ;;;; (forward-line 1). This would require a number of changes to dired.el.
  4879. ;;;; If dired gets confused, revert-buffer will fix it.
  4880.  
  4881. ;;(defun ange-ftp-dired-vms-ls-trim ()
  4882. ;;  (goto-char (point-min))
  4883. ;;  (let ((case-fold-search nil))
  4884. ;;    (re-search-forward  ange-ftp-vms-filename-regexp))
  4885. ;;  (beginning-of-line)
  4886. ;;  (delete-region (point-min) (point))
  4887. ;;  (forward-line 1)
  4888. ;;  (delete-region (point) (point-max)))
  4889.  
  4890.  
  4891. ;;(or (assq 'vms ange-ftp-dired-ls-trim-alist)
  4892. ;;    (setq ange-ftp-dired-ls-trim-alist
  4893. ;;      (cons '(vms . ange-ftp-dired-vms-ls-trim)
  4894. ;;        ange-ftp-dired-ls-trim-alist)))    
  4895.  
  4896. (defun ange-ftp-vms-sans-version (name &rest args)
  4897.   (save-match-data
  4898.     (if (string-match ";[0-9]+$" name)
  4899.     (substring name 0 (match-beginning 0))
  4900.       name)))
  4901.  
  4902. (or (assq 'vms ange-ftp-sans-version-alist)
  4903.     (setq ange-ftp-sans-version-alist
  4904.       (cons '(vms . ange-ftp-vms-sans-version)
  4905.         ange-ftp-sans-version-alist)))
  4906.  
  4907. ;;(defvar ange-ftp-file-version-alist)
  4908.  
  4909. ;;;;; The vms version of clean-directory has 2 more optional args
  4910. ;;;;; than the usual dired version. This is so that it can be used by
  4911. ;;;;; ange-ftp-dired-vms-flag-backup-files.
  4912.  
  4913. ;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
  4914. ;;  "Flag numerical backups for deletion.
  4915. ;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  4916. ;;Positive prefix arg KEEP overrides `dired-kept-versions';
  4917. ;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  4918.  
  4919. ;;To clear the flags on these files, you can use \\[dired-flag-backup-files]
  4920. ;;with a prefix argument."
  4921. ;;;  (interactive "P") ; Never actually called interactively.
  4922. ;;  (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
  4923. ;;  (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
  4924. ;;    ;; late-retention must NEVER be allowed to be less than 1 in VMS!
  4925. ;;    ;; This could wipe ALL copies of the file.
  4926. ;;    (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
  4927. ;;    (action (or msg "Cleaning"))
  4928. ;;    (ange-ftp-trample-marker (or marker dired-del-marker))
  4929. ;;    (ange-ftp-file-version-alist ()))
  4930. ;;    (message (concat action
  4931. ;;             " numerical backups (keeping %d late, %d old)...")
  4932. ;;         late-retention early-retention)
  4933. ;;    ;; Look at each file.
  4934. ;;    ;; If the file has numeric backup versions,
  4935. ;;    ;; put on ange-ftp-file-version-alist an element of the form
  4936. ;;    ;; (FILENAME . VERSION-NUMBER-LIST)
  4937. ;;    (dired-map-dired-file-lines (function
  4938. ;;                 ange-ftp-dired-vms-collect-file-versions))
  4939. ;;    ;; Sort each VERSION-NUMBER-LIST,
  4940. ;;    ;; and remove the versions not to be deleted.
  4941. ;;    (let ((fval ange-ftp-file-version-alist))
  4942. ;;      (while fval
  4943. ;;    (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
  4944. ;;           (v-count (length sorted-v-list)))
  4945. ;;      (if (> v-count (+ early-retention late-retention))
  4946. ;;          (rplacd (nthcdr early-retention sorted-v-list)
  4947. ;;              (nthcdr (- v-count late-retention)
  4948. ;;                  sorted-v-list)))
  4949. ;;      (rplacd (car fval)
  4950. ;;          (cdr sorted-v-list)))
  4951. ;;    (setq fval (cdr fval))))
  4952. ;;    ;; Look at each file.  If it is a numeric backup file,
  4953. ;;    ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
  4954. ;;    (dired-map-dired-file-lines
  4955. ;;     (function
  4956. ;;      ange-ftp-dired-vms-trample-file-versions mark))
  4957. ;;    (message (concat action " numerical backups...done"))))
  4958.  
  4959. ;;(or (assq 'vms ange-ftp-dired-clean-directory-alist)
  4960. ;;    (setq ange-ftp-dired-clean-directory-alist
  4961. ;;      (cons '(vms . ange-ftp-dired-vms-clean-directory)
  4962. ;;        ange-ftp-dired-clean-directory-alist)))
  4963.  
  4964. ;;(defun ange-ftp-dired-vms-collect-file-versions (fn)
  4965. ;;  ;;  "If it looks like file FN has versions, return a list of the versions.
  4966. ;;  ;;That is a list of strings which are file names.
  4967. ;;  ;;The caller may want to flag some of these files for deletion."
  4968. ;;(let ((name (nth 2 (ange-ftp-ftp-name fn))))
  4969. ;;  (if (string-match ";[0-9]+$" name)
  4970. ;;      (let* ((name (substring name 0 (match-beginning 0)))
  4971. ;;         (fn (ange-ftp-replace-name-component fn name)))
  4972. ;;    (if (not (assq fn ange-ftp-file-version-alist))
  4973. ;;        (let* ((base-versions
  4974. ;;            (concat (file-name-nondirectory name) ";"))
  4975. ;;           (bv-length (length base-versions))
  4976. ;;           (possibilities (file-name-all-completions
  4977. ;;                   base-versions
  4978. ;;                   (file-name-directory fn)))
  4979. ;;           (versions (mapcar
  4980. ;;                  '(lambda (arg)
  4981. ;;                 (if (and (string-match
  4982. ;;                       "[0-9]+$" arg bv-length)
  4983. ;;                      (= (match-beginning 0) bv-length))
  4984. ;;                     (string-to-int (substring arg bv-length))
  4985. ;;                   0))
  4986. ;;                  possibilities)))
  4987. ;;          (if versions
  4988. ;;          (setq
  4989. ;;           ange-ftp-file-version-alist
  4990. ;;           (cons (cons fn versions)
  4991. ;;             ange-ftp-file-version-alist)))))))))
  4992.  
  4993. ;;(defun ange-ftp-dired-vms-trample-file-versions (fn)
  4994. ;;  (let* ((start-vn (string-match ";[0-9]+$" fn))
  4995. ;;     base-version-list)
  4996. ;;    (and start-vn
  4997. ;;     (setq base-version-list    ; there was a base version to which
  4998. ;;           (assoc (substring fn 0 start-vn)    ; this looks like a
  4999. ;;              ange-ftp-file-version-alist))    ; subversion
  5000. ;;     (not (memq (string-to-int (substring fn (1+ start-vn)))
  5001. ;;            base-version-list))    ; this one doesn't make the cut
  5002. ;;     (progn (beginning-of-line)
  5003. ;;        (delete-char 1)
  5004. ;;        (insert ange-ftp-trample-marker)))))
  5005.  
  5006. ;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
  5007. ;;  (let ((dired-kept-versions 1)
  5008. ;;    (kept-old-versions 0)
  5009. ;;    marker msg)
  5010. ;;    (if unflag-p
  5011. ;;    (setq marker ?\040 msg "Unflagging")
  5012. ;;      (setq marker dired-del-marker msg "Cleaning"))
  5013. ;;    (ange-ftp-dired-vms-clean-directory nil marker msg)))
  5014.  
  5015. ;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
  5016. ;;    (setq ange-ftp-dired-flag-backup-files-alist
  5017. ;;      (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
  5018. ;;        ange-ftp-dired-flag-backup-files-alist)))
  5019.  
  5020. ;;(defun ange-ftp-dired-vms-backup-diff (&optional switches)
  5021. ;;  (let ((file (dired-get-filename 'no-dir))
  5022. ;;    bak)
  5023. ;;    (if (and (string-match ";[0-9]+$" file)
  5024. ;;         ;; Find most recent previous version.
  5025. ;;         (let ((root (substring file 0 (match-beginning 0)))
  5026. ;;           (ver
  5027. ;;            (string-to-int (substring file (1+ (match-beginning 0)))))
  5028. ;;           found)
  5029. ;;           (setq ver (1- ver))
  5030. ;;           (while (and (> ver 0) (not found))
  5031. ;;         (setq bak (concat root ";" (int-to-string ver)))
  5032. ;;         (and (file-exists-p bak) (setq found t))
  5033. ;;         (setq ver (1- ver)))
  5034. ;;           found))
  5035. ;;    (if switches
  5036. ;;        (diff (expand-file-name bak) (expand-file-name file) switches)
  5037. ;;      (diff (expand-file-name bak) (expand-file-name file)))
  5038. ;;      (error "No previous version found for %s" file))))
  5039.  
  5040. ;;(or (assq 'vms ange-ftp-dired-backup-diff-alist)
  5041. ;;    (setq ange-ftp-dired-backup-diff-alist
  5042. ;;      (cons '(vms . ange-ftp-dired-vms-backup-diff)
  5043. ;;        ange-ftp-dired-backup-diff-alist)))
  5044.  
  5045.  
  5046. ;;;; ------------------------------------------------------------
  5047. ;;;; MTS support
  5048. ;;;; ------------------------------------------------------------
  5049.  
  5050.  
  5051. ;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from
  5052. ;; MTS to UNIX-ish.
  5053. (defun ange-ftp-fix-name-for-mts (name &optional reverse)
  5054.   (save-match-data
  5055.     (if reverse
  5056.     (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name)
  5057.         (let (acct file)
  5058.           (if (match-beginning 1)
  5059.           (setq acct (substring name 0 (match-end 1))))
  5060.           (if (match-beginning 2)
  5061.           (setq file (substring name
  5062.                     (match-beginning 2) (match-end 2))))
  5063.           (concat (and acct (concat "/" acct "/"))
  5064.               file))
  5065.       (error "name %s didn't match" name))
  5066.       (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name)
  5067.       (concat (substring name 1 (match-end 1))
  5068.           (substring name (match-beginning 2) (match-end 2)))
  5069.     ;; Let's hope that mts will recognize it anyway.
  5070.     name))))
  5071.  
  5072. (or (assq 'mts ange-ftp-fix-name-func-alist)
  5073.     (setq ange-ftp-fix-name-func-alist
  5074.       (cons '(mts . ange-ftp-fix-name-for-mts)
  5075.         ange-ftp-fix-name-func-alist)))
  5076.  
  5077. ;; Convert name from UNIX-ish to MTS ready for a DIRectory listing.
  5078. ;; Remember that there are no directories in MTS.
  5079. (defun ange-ftp-fix-dir-name-for-mts (dir-name)
  5080.   (if (string-equal dir-name "/")
  5081.       (error "Cannot get listing for fictitious \"/\" directory.")
  5082.     (let ((dir-name (ange-ftp-fix-name-for-mts dir-name)))
  5083.       (cond
  5084.        ((string-equal dir-name "")
  5085.     "?")
  5086.        ((string-match ":$" dir-name)
  5087.     (concat dir-name "?"))
  5088.        (dir-name))))) ; It's just a single file.
  5089.  
  5090. (or (assq 'mts ange-ftp-fix-dir-name-func-alist)
  5091.     (setq ange-ftp-fix-dir-name-func-alist
  5092.       (cons '(mts . ange-ftp-fix-dir-name-for-mts)
  5093.         ange-ftp-fix-dir-name-func-alist)))
  5094.  
  5095. (or (memq 'mts ange-ftp-dumb-host-types)
  5096.     (setq ange-ftp-dumb-host-types
  5097.       (cons 'mts ange-ftp-dumb-host-types)))
  5098.  
  5099. (defvar ange-ftp-mts-host-regexp nil)
  5100.  
  5101. ;; Return non-nil if HOST is running MTS.
  5102. (defun ange-ftp-mts-host (host)
  5103.   (and ange-ftp-mts-host-regexp
  5104.        (save-match-data
  5105.      (string-match ange-ftp-mts-host-regexp host))))
  5106.  
  5107. ;; Parse the current buffer which is assumed to be in mts ftp dir format.
  5108. (defun ange-ftp-parse-mts-listing ()
  5109.   (let ((tbl (ange-ftp-make-hashtable)))
  5110.     (goto-char (point-min))
  5111.     (save-match-data
  5112.       (while (re-search-forward ange-ftp-date-regexp nil t)
  5113.     (end-of-line)
  5114.     (skip-chars-backward " ")
  5115.     (let ((end (point)))
  5116.       (skip-chars-backward "-A-Z0-9_.!")
  5117.       (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl))
  5118.     (forward-line 1)))
  5119.       ;; Don't need to bother with ..
  5120.     (ange-ftp-put-hash-entry "." t tbl)
  5121.     tbl))
  5122.  
  5123. (or (assq 'mts ange-ftp-parse-list-func-alist)
  5124.     (setq ange-ftp-parse-list-func-alist
  5125.       (cons '(mts . ange-ftp-parse-mts-listing)
  5126.         ange-ftp-parse-list-func-alist)))
  5127.  
  5128. (defun ange-ftp-add-mts-host (host)
  5129.   "Mark HOST as the name of a machine running MTS."
  5130.   (interactive
  5131.    (list (read-string "Host: "
  5132.               (let ((name (or (buffer-file-name) default-directory)))
  5133.             (and name (car (ange-ftp-ftp-name name)))))))
  5134.   (if (not (ange-ftp-mts-host host))
  5135.       (setq ange-ftp-mts-host-regexp
  5136.         (concat "^" (regexp-quote host) "$"
  5137.             (and ange-ftp-mts-host-regexp "\\|")
  5138.             ange-ftp-mts-host-regexp)
  5139.         ange-ftp-host-cache nil)))
  5140.  
  5141. ;;; Tree dired support:
  5142.  
  5143. ;;;; There aren't too many systems left that use MTS. This dired support will
  5144. ;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
  5145. ;;;; implement ftp in the same way. If not, it might be necessary to make the
  5146. ;;;; following more flexible.
  5147.  
  5148. ;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
  5149. ;;  "In dired, move to first char of filename on this line.
  5150. ;;Returns position (point) or nil if no filename on this line."
  5151. ;;  ;; This is the MTS version.
  5152. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  5153. ;;  (beginning-of-line)
  5154. ;;  (if (re-search-forward
  5155. ;;       ange-ftp-date-regexp eol t)
  5156. ;;      (progn
  5157. ;;    (skip-chars-forward " ")          ; Eat blanks after date
  5158. ;;    (skip-chars-forward "0-9:" eol)   ; Eat time or year
  5159. ;;    (skip-chars-forward " " eol)      ; one space before filename
  5160. ;;    ;; When listing an account other than the users own account it appends
  5161. ;;    ;; ACCT: to the beginning of the filename. Skip over this.
  5162. ;;    (and (looking-at "[A-Z0-9_.]+:")
  5163. ;;         (goto-char (match-end 0)))
  5164. ;;    (point))
  5165. ;;    (if raise-error
  5166. ;;    (error "No file on this line")
  5167. ;;      nil)))
  5168.  
  5169. ;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist)
  5170. ;;    (setq ange-ftp-dired-move-to-filename-alist
  5171. ;;      (cons '(mts . ange-ftp-dired-mts-move-to-filename)
  5172. ;;        ange-ftp-dired-move-to-filename-alist)))
  5173.  
  5174. ;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
  5175. ;;  ;; Assumes point is at beginning of filename.
  5176. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  5177. ;;  ;; On failure, signals an error or returns nil.
  5178. ;;  ;; This is the MTS version.
  5179. ;;  (let (opoint hidden case-fold-search)
  5180. ;;    (setq opoint (point)
  5181. ;;      eol (save-excursion (end-of-line) (point))
  5182. ;;      hidden (and selective-display
  5183. ;;              (save-excursion (search-forward "\r" eol t))))
  5184. ;;    (if hidden
  5185. ;;    nil
  5186. ;;      (skip-chars-forward "-A-Z0-9._!" eol))
  5187. ;;    (or no-error
  5188. ;;    (not (eq opoint (point)))
  5189. ;;    (error
  5190. ;;     (if hidden
  5191. ;;         (substitute-command-keys
  5192. ;;          "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5193. ;;       "No file on this line")))
  5194. ;;    (if (eq opoint (point))
  5195. ;;    nil
  5196. ;;      (point))))
  5197.  
  5198. ;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
  5199. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  5200. ;;      (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
  5201. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  5202.  
  5203. ;;;; ------------------------------------------------------------
  5204. ;;;; CMS support
  5205. ;;;; ------------------------------------------------------------
  5206.  
  5207. ;; Since CMS doesn't have any full file name syntax, we have to fudge
  5208. ;; things with cd's. We actually send too many cd's, but it's dangerous
  5209. ;; to try to remember the current minidisk, because if the connection
  5210. ;; is closed and needs to be reopened, we will find ourselves back in
  5211. ;; the default minidisk. This is fairly likely since CMS ftp servers
  5212. ;; usually close the connection after 5 minutes of inactivity.
  5213.  
  5214. ;; Have I got the filename character set right?
  5215.  
  5216. (defun ange-ftp-fix-name-for-cms (name &optional reverse)
  5217.   (save-match-data
  5218.     (if reverse
  5219.     ;; Since we only convert output from a pwd in this direction,
  5220.     ;; we'll assume that it's a minidisk, and make it into a
  5221.     ;; directory file name. Note that the expand-dir-hashtable
  5222.     ;; stores directories without the trailing /. Is this
  5223.     ;; consistent?
  5224.     (concat "/" name)
  5225.       (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
  5226.             name)
  5227.       (let ((minidisk (substring name 1 (match-end 1))))
  5228.         (if (match-beginning 2)
  5229.         (let ((file (substring name (match-beginning 2)
  5230.                        (match-end 2)))
  5231.               (cmd (concat "cd " minidisk))
  5232.  
  5233.               ;; Note that host and user are bound in the call
  5234.               ;; to ange-ftp-send-cmd
  5235.               (proc (ange-ftp-get-process ange-ftp-this-host
  5236.                           ange-ftp-this-user)))
  5237.  
  5238.           ;; Must use ange-ftp-raw-send-cmd here to avoid
  5239.           ;; an infinite loop.
  5240.           (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg))
  5241.               file
  5242.             ;; failed... try ONCE more.
  5243.             (setq proc (ange-ftp-get-process ange-ftp-this-host
  5244.                              ange-ftp-this-user))
  5245.             (let ((result (ange-ftp-raw-send-cmd proc cmd
  5246.                              ange-ftp-this-msg)))
  5247.               (if (car result)
  5248.               file
  5249.             ;; failed.  give up.
  5250.             (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
  5251.                     (format "cd to minidisk %s failed: %s"
  5252.                         minidisk (cdr result)))))))
  5253.           ;; return the minidisk
  5254.           minidisk))
  5255.     (error "Invalid CMS filename")))))
  5256.  
  5257. (or (assq 'cms ange-ftp-fix-name-func-alist)
  5258.     (setq ange-ftp-fix-name-func-alist
  5259.       (cons '(cms . ange-ftp-fix-name-for-cms)
  5260.         ange-ftp-fix-name-func-alist)))
  5261.  
  5262. (or (memq 'cms ange-ftp-dumb-host-types)
  5263.     (setq ange-ftp-dumb-host-types
  5264.       (cons 'cms ange-ftp-dumb-host-types)))
  5265.  
  5266. ;; Convert name from UNIX-ish to CMS ready for a DIRectory listing.
  5267. (defun ange-ftp-fix-dir-name-for-cms (dir-name)
  5268.   (cond
  5269.    ((string-equal "/" dir-name)
  5270.     (error "Cannot get listing for fictitious \"/\" directory."))
  5271.    ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name)
  5272.     (let* ((minidisk (substring dir-name (match-beginning 1) (match-end 1)))
  5273.        ;; host and user are bound in the call to ange-ftp-send-cmd
  5274.        (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user))
  5275.        (cmd (concat "cd " minidisk))
  5276.        (file (if (match-beginning 2)
  5277.              ;; it's a single file
  5278.              (substring dir-name (match-beginning 2)
  5279.                 (match-end 2))
  5280.            ;; use the wild-card
  5281.            "*")))
  5282.       (if (car (ange-ftp-raw-send-cmd proc cmd))
  5283.       file
  5284.     ;; try again...
  5285.     (setq proc (ange-ftp-get-process ange-ftp-this-host
  5286.                      ange-ftp-this-user))
  5287.     (let ((result (ange-ftp-raw-send-cmd proc cmd)))
  5288.       (if (car result)
  5289.           file
  5290.         ;; give up
  5291.         (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
  5292.                 (format "cd to minidisk %s failed: %s"
  5293.                     minidisk (cdr result))))))))
  5294.    (t (error "Invalid CMS file name"))))
  5295.  
  5296. (or (assq 'cms ange-ftp-fix-dir-name-func-alist)
  5297.     (setq ange-ftp-fix-dir-name-func-alist
  5298.       (cons '(cms . ange-ftp-fix-dir-name-for-cms)
  5299.         ange-ftp-fix-dir-name-func-alist)))
  5300.  
  5301. (defvar ange-ftp-cms-host-regexp nil
  5302.   "Regular expression to match hosts running the CMS operating system.")
  5303.  
  5304. ;; Return non-nil if HOST is running CMS.
  5305. (defun ange-ftp-cms-host (host)
  5306.   (and ange-ftp-cms-host-regexp
  5307.        (save-match-data
  5308.      (string-match ange-ftp-cms-host-regexp host))))
  5309.  
  5310. (defun ange-ftp-add-cms-host (host)
  5311.   "Mark HOST as the name of a CMS host."
  5312.   (interactive
  5313.    (list (read-string "Host: "
  5314.               (let ((name (or (buffer-file-name) default-directory)))
  5315.             (and name (car (ange-ftp-ftp-name name)))))))
  5316.   (if (not (ange-ftp-cms-host host))
  5317.       (setq ange-ftp-cms-host-regexp
  5318.         (concat "^" (regexp-quote host) "$"
  5319.             (and ange-ftp-cms-host-regexp "\\|")
  5320.             ange-ftp-cms-host-regexp)
  5321.         ange-ftp-host-cache nil)))
  5322.  
  5323. (defun ange-ftp-parse-cms-listing ()
  5324.   ;; Parse the current buffer which is assumed to be a CMS directory listing.
  5325.   ;; If we succeed in getting a listing, then we will assume that the minidisk
  5326.   ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
  5327.   ;; because ange-ftp doesn't know that the root hashtable has only part of
  5328.   ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
  5329.   ;; exist. It would be nice if completion worked for minidisks, as we
  5330.   ;; discover them.
  5331. ;  (let* ((dir-file (directory-file-name file))
  5332. ;     (root (file-name-directory dir-file))
  5333. ;     (minidisk (ange-ftp-get-file-part dir-file))
  5334. ;     (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
  5335. ;    (if root-tbl
  5336. ;    (ange-ftp-put-hash-entry minidisk t root-tbl)
  5337. ;      (setq root-tbl (ange-ftp-make-hashtable))
  5338. ;      (ange-ftp-put-hash-entry minidisk t root-tbl)
  5339. ;      (ange-ftp-put-hash-entry "." t root-tbl)
  5340. ;      (ange-ftp-set-files root root-tbl)))
  5341.   ;; Now do the usual parsing
  5342.   (let ((tbl (ange-ftp-make-hashtable)))
  5343.     (goto-char (point-min))
  5344.     (save-match-data
  5345.       (while
  5346.       (re-search-forward
  5347.        "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
  5348.     (ange-ftp-put-hash-entry
  5349.      (concat (buffer-substring (match-beginning 1)
  5350.                    (match-end 1))
  5351.          "."
  5352.          (buffer-substring (match-beginning 2)
  5353.                    (match-end 2)))
  5354.      nil tbl)
  5355.     (forward-line 1))
  5356.       (ange-ftp-put-hash-entry "." t tbl))
  5357.     tbl))
  5358.  
  5359. (or (assq 'cms ange-ftp-parse-list-func-alist)
  5360.     (setq ange-ftp-parse-list-func-alist
  5361.       (cons '(cms . ange-ftp-parse-cms-listing)
  5362.         ange-ftp-parse-list-func-alist)))
  5363.     
  5364. ;;;;; Tree dired support:
  5365.  
  5366. ;;(defconst ange-ftp-dired-cms-re-exe
  5367. ;;  "^. [-A-Z0-9$_]+ +EXEC "
  5368. ;;  "Regular expression to use to search for CMS executables.")
  5369.  
  5370. ;;(or (assq 'cms ange-ftp-dired-re-exe-alist)
  5371. ;;    (setq ange-ftp-dired-re-exe-alist
  5372. ;;      (cons (cons 'cms  ange-ftp-dired-cms-re-exe)
  5373. ;;        ange-ftp-dired-re-exe-alist)))
  5374.  
  5375.  
  5376. ;;(defun ange-ftp-dired-cms-insert-headerline (dir)
  5377. ;;  ;; CMS has no total line, so we insert a blank line for
  5378. ;;  ;; aesthetics.
  5379. ;;  (insert "\n")
  5380. ;;  (forward-char -1)
  5381. ;;  (ange-ftp-real-dired-insert-headerline dir))
  5382.  
  5383. ;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist)
  5384. ;;    (setq ange-ftp-dired-insert-headerline-alist
  5385. ;;      (cons '(cms . ange-ftp-dired-cms-insert-headerline)
  5386. ;;        ange-ftp-dired-insert-headerline-alist)))
  5387.  
  5388. ;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
  5389. ;;  "In dired, move to the first char of filename on this line."
  5390. ;;  ;; This is the CMS version.
  5391. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  5392. ;;  (let (case-fold-search)
  5393. ;;    (beginning-of-line)
  5394. ;;    (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
  5395. ;;    (goto-char (1+ (match-beginning 0)))
  5396. ;;      (if raise-error
  5397. ;;      (error "No file on this line")
  5398. ;;    nil))))
  5399.  
  5400. ;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist)
  5401. ;;    (setq ange-ftp-dired-move-to-filename-alist
  5402. ;;      (cons '(cms . ange-ftp-dired-cms-move-to-filename)
  5403. ;;        ange-ftp-dired-move-to-filename-alist)))
  5404.  
  5405. ;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
  5406. ;;  ;; Assumes point is at beginning of filename.
  5407. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  5408. ;;  ;; case-fold-search must be nil, at least for VMS.
  5409. ;;  ;; On failure, signals an error or returns nil.
  5410. ;;  ;; This is the CMS version.
  5411. ;;  (let ((opoint (point))
  5412. ;;    case-fold-search hidden)
  5413. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  5414. ;;    (setq hidden (and selective-display
  5415. ;;              (save-excursion
  5416. ;;            (search-forward "\r" eol t))))
  5417. ;;    (if hidden
  5418. ;;    (if no-error
  5419. ;;        nil
  5420. ;;      (error
  5421. ;;       (substitute-command-keys
  5422. ;;        "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  5423. ;;      (skip-chars-forward "-A-Z0-9$_" eol)
  5424. ;;      (skip-chars-forward " " eol)
  5425. ;;      (skip-chars-forward "-A-Z0-9$_" eol)
  5426. ;;      (if (eq opoint (point))
  5427. ;;      (if no-error
  5428. ;;          nil
  5429. ;;        (error "No file on this line"))
  5430. ;;    (point)))))
  5431.  
  5432. ;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
  5433. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  5434. ;;      (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
  5435. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  5436.  
  5437. (defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
  5438.   (if (string-match "-Z$" name)
  5439.       (list nil (substring name 0 -2))
  5440.     (list t (concat name "-Z"))))
  5441.  
  5442. (or (assq 'cms ange-ftp-make-compressed-filename-alist)
  5443.     (setq ange-ftp-make-compressed-filename-alist
  5444.       (cons '(cms . ange-ftp-cms-make-compressed-filename)
  5445.         ange-ftp-make-compressed-filename-alist)))
  5446.  
  5447. ;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
  5448. ;;  (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
  5449. ;;    (and name
  5450. ;;     (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
  5451. ;;         (concat (substring name 0 (match-end 1))
  5452. ;;             "."
  5453. ;;             (substring name (match-beginning 2) (match-end 2)))
  5454. ;;       name))))
  5455.  
  5456. ;;(or (assq 'cms ange-ftp-dired-get-filename-alist)
  5457. ;;    (setq ange-ftp-dired-get-filename-alist
  5458. ;;      (cons '(cms . ange-ftp-dired-cms-get-filename)
  5459. ;;        ange-ftp-dired-get-filename-alist)))
  5460.  
  5461. ;;;; ------------------------------------------------------------
  5462. ;;;; Finally provide package.
  5463. ;;;; ------------------------------------------------------------
  5464.  
  5465. (provide 'ange-ftp)
  5466.  
  5467. ;;; ange-ftp.el ends here
  5468.